Category Archives: H-Bridge

H-Bridge Circuit Control Using Arduino and BananaPi

by : Antonius (@sw0rdm4n)
http://www.ringlayer.net

H-bridge is an electronic circuit which allow you to reverse the flow of electron in either direction.
To control a h-bridge circuit here, we’ll be using an arduino mega 2560 and arduino ethernet shield (that suitable for arduino mega 2560 with no electronic hacks). What we’re gonna do here is simply can be explained using below diagram :

scheme

 

The main purpose is to send some command(s) such as : forward / backward / left or right from banana pi to arduino mega. Then arduino mega will receives the command over ethernet and apply the suitable voltage over it’s pin(s) for h-bridge. The h-bridge then will apply suitable load for those 2 motor dc(s).

Communication Between Arduino and Banana PI or Raspberry PI or beagle bone to arduino can be accomplished using several method(s) such as using logic level converter (https://www.sparkfun.com/products/12009) or cape (for beagle bone) or maybe you can use arduino wifi shield and plug some wireless usb adapter to credit card size board. Within this example, we’ll be using ethernet to communicate between arduino and banana pi. Why use ethernet shield ? the programming will be easier, what you need is a knowledge on socket programming.

Hardware Requirements:

– 1 banana pi or raspberry pi or another credit card size board with linux os installed

– 1 arduino mega 2560

– 1 ethernet shield for arduino mega 2560

– 1 half size breadboard

– some jumper wire(s)

– 1 sn75441one or l293dne (h-bridge ic)

– 2 motor dc

Step 1 . Prepare H-Bridge circuit

Before making h-bridge circuit, plug the ethernet shield to arduino mega. So the pin(s) will be plugged on the ethernet shield.

In order to make h-bridge circuit, you can follow these tutorials:

– http://www.instructables.com/id/Duel-Motor-Driver-with-Arduino-using-a-SN754410NE-/

– https://itp.nyu.edu/physcomp/labs/motors-and-transistors/dc-motor-control-using-an-h-bridge/

Here comes the arduino mega 2560 with ethernet shield connected to h-bridge circuit :

IMG_20150512_003504

Step 2. Prepare the code for arduino

Here comes the source code

/* arduino mega 2560 control receive command over ethernet and apply voltage to h-bridge circuit 
source code made by : Antonius (Sw0rdm4n)
*/
#include <SPI.h>
#include <Ethernet.h> 

byte mac[] = { 0xDE, 0xA2, 0xDA, 0x0F, 0x17, 0xD9 }; 
IPAddress ip(10,0,0,3); 
IPAddress gateway(10,0,0,2); 
IPAddress subnet(255,128,0,0); 
EthernetServer server(23); 
boolean alreadyConnected = false;

/* h-bridge code for arduino Modified from http://itp.nyu.edu/physcomp/Labs/DCMotorControl */

int motorkanan_1 = 6;    
int motorkanan_2 = 7;   
int enablePin_1 = 9;   
int enablePin_2 = 8;   
int motorkiri_1 = 5;    
int motorkiri_2 = 4;    
int half_speed = 127;
int slow_sleep = 60;
int full_speed = 255;
int i = 0;
#define fwd 1
#define bwd 2
#define lft 3
#define rht 4
#define stp 5
int last_cmd = 0;

void setup() {
    Ethernet.begin(mac, ip);   
    server.begin(); 
    Serial.begin(9600);
    Serial.println("\nrobot motoric started\n");
    pinMode(motorkanan_1, OUTPUT); 
    pinMode(motorkanan_2, OUTPUT); 
    pinMode(motorkiri_1, OUTPUT); 
    pinMode(motorkiri_2, OUTPUT); 
    digitalWrite(enablePin_1, HIGH); 
    digitalWrite(enablePin_2, HIGH); 
}
void tfwd()
{
      Serial.println("\nmaju\n");
      digitalWrite(motorkanan_1, LOW);  
      digitalWrite(motorkanan_2, HIGH);  
      digitalWrite(motorkiri_1, LOW);   
      digitalWrite(motorkiri_2, HIGH);  
}
void tstop()
{
    Serial.println("stop");

    digitalWrite(motorkanan_1, LOW);  
    digitalWrite(motorkanan_2, LOW);
      
    digitalWrite(motorkiri_1, LOW);  
    digitalWrite(motorkiri_2, LOW);
    delay(1000);
}
void tback()
{
      Serial.println("\nmundur\n");
      digitalWrite(motorkanan_1, HIGH);   //right
      digitalWrite(motorkanan_2, LOW);
      digitalWrite(motorkiri_1, HIGH);   //left 
      digitalWrite(motorkiri_2, LOW);
}
void tleft()
{
      Serial.println("left");
      digitalWrite(motorkanan_1, HIGH);  
      digitalWrite(motorkanan_2, LOW);  
      digitalWrite(motorkiri_1, LOW);  
      digitalWrite(motorkiri_2, HIGH);
      delay(1000);
      tstop(); 
      last_cmd = 0; 
}
void tright()
{
      Serial.println("right");
      digitalWrite(motorkanan_1, LOW);  
      digitalWrite(motorkanan_2, HIGH);  
      digitalWrite(motorkiri_1, HIGH);   
      digitalWrite(motorkiri_2, LOW);
      delay(1000);
      tstop(); 
      last_cmd = 0;
}


void get_ethernet_cmd()
{
    char cmd;
    
    /* modified from arduino ethernet chat server */ 
    EthernetClient client = server.available();
    if (client) {
        if (!alreadyConnected) {
              client.flush();    
              Serial.print("\nReady !\n");
              client.println("motoric ready to receive command !"); 
              alreadyConnected = true;
        }
        if (client.available() > 0) {
               Serial.print("reading from client");
               cmd = client.read();
               Serial.print("\ngot command:");
               Serial.print(cmd);
               switch (cmd) {
                   case 'f':
                   last_cmd = fwd;
                   delay(100);    
                   break;
                   case 'b':
                   last_cmd = bwd;
                   delay(100);      
                   break;
                   case 's':
                   last_cmd = stp;
                   break;
                   case 'l':
                   last_cmd = lft;
                   break;
                   case 'r':
                   last_cmd = rht;               
                   break;
               }
        } 
    } 
}

void net_op()
{
   get_ethernet_cmd();
   if (last_cmd == 0) {
      tstop(); 
   }
   else {
      switch (last_cmd) {
        case fwd:
        tfwd();
        break;
        case bwd:
        tback();
        break;
        case stp:
        tstop();
        break;
        case lft:
        tleft();
        break;
        case rht:
        tright();
        break;
      } 
   } 
  
}
void loop() {
     net_op();
}

Step 3. Plug rj45 to arduino and connect it to banana pi

Once you have done everything correctly, we can control h-bridge circuit using banana pi.
Based on above source, arduino ip address is at 10.0.0.3 where it’s gateway is 10.0.0.2 (our banana pi). From banana pi terminal what to do is simple:
ifconfig eth0 10.0.0.2 netmask 255.128.0.0 up

Now, from banana pi terminal, we can send  some command(s) to make our 2 motor dc(s) move forward, backward, left or right.

From terminal, type :

telnet 10.0.0.3

Then we’ll be connected to arduino mega 2560

In order to make motor dc moves forward, type: f

In order to make motor dc moves backward, type: b

In order to make motor dc moves left, type: l

In order to make motor dc moves right, type: r

In order to make motor dc stop, type: s

 

 

 

 

 

 

Advertisements

Wheeled Robot Motoric With 3 Ultrasonic Sensors

by : Antonius (@sw0rdm4n)
http://www,ringlayer.net

robot

A. PART LISTS

– 1 sn75441one or l293dne (h-bridge ic)
l293d

– 1 Arduino Uno

– 3 HC-SR04 Ultrasonic Sensor (Front, Left and Right Sensor)

– Arduino smart Car Robot Plastic Tire Wheel with DC 3-6v Gear Motor
x

– Any Wheeled Chassis

You can make any chassis easily using acrylic, For example here:

20141231_120614 20141231_120621

For this robot, I modify my kitchen plastic supplies in order to make my own wheeled chassis:

20141231_120639

– Mini Trolley Wheel (Front Wheel)

mini

– Half Size Breadboard

– Jumper Wires

– 9 volt / 8,4 volt battery

 – Some Bauds

 

B. H-BRIDGE CIRCUIT

In order to make a robot who can avoid front, left and right obstable. We need to make a robot who can turn right, left and turn back.  For that reason, we need a circuit that can switch direction of voltage.  For that, we can use h-bridge, For example here, we can use sn75441one or l293dne. Since a motor dc can reverse direction of spins when a voltage switch to other direction. To make a h-bridge circuit, you can follow this tutorial : http://www.instructables.com/id/Duel-Motor-Driver-with-Arduino-using-a-SN754410NE-/

The method to turn right or left is easy, this robot actually ony has 2 wheel for motoric : left wheel and right wheel.  In order to turn left, right wheel must turn forward and left wheel must turn backward. Meanwhile, in order to turn right, left wheel must turn forward and right wheel must turn backward.

C. HC-SR04 ULTRASONIC SENSOR

HC-SR04 works based on this formula : Speed = Distance / Time. This sensor works by sending a ping of ultrasonic sensor, then calculate. In order to use hc-sr04 we can use NewPing library taken from here : http://code.google.com/p/arduino-new-ping/

For front sensor, connect trig pin to pin 12 on arduino, connect echo pin to pin 13 on arduino.

For left sensor, connect trig pin to pin 10 on arduino, connect echo pin to pin 11 on arduino.

For right sensor, connect trig pin to pin 6 on arduino, connect echo pin to pin 7 on arduino.

Here’s the code for arduino :

/*
Simple wheeled robot motoric with 3 sensor(s) - front, left and right
Made by : Antonius (sw0rdm4n)
http://www.ringlayer.net
*/
#include <NewPing.h>

/* front sonar */
#define TRIG_DEPAN_ATAS  12 
#define ECHO_DEPAN_ATAS     13  

/* left sonar */
#define TRIG_KIRI  10 
#define ECHO_KIRI     11  

/* right sonar */
#define TRIG_KANAN  6 
#define ECHO_KANAN    7  

#define MAX_DISTANCE 200 
NewPing sonar(TRIG_DEPAN_ATAS, ECHO_DEPAN_ATAS, MAX_DISTANCE); 
NewPing sonar_kiri(TRIG_KIRI, ECHO_KIRI, MAX_DISTANCE); 
NewPing sonar_kanan(TRIG_KANAN, ECHO_KANAN, MAX_DISTANCE); 

/* Modified from http://itp.nyu.edu/physcomp/Labs/DCMotorControl */

int motorkanan_arah_depan = 4;    
int motorkanan_arah_belakang = 5;   
int enablePin = 9;   
int motorkiri_arah_depan = 2;    
int motorkiri_arah_belakang = 3;    
unsigned int sensor1_val = 0;
unsigned int sensorkiri_val = 0;
unsigned int sensorkanan_val= 0;
int i = 0;

void setup() {
    Serial.begin(9600);
    pinMode(motorkanan_arah_depan, OUTPUT); 
    pinMode(motorkanan_arah_belakang, OUTPUT); 
    pinMode(motorkiri_arah_depan, OUTPUT); 
    pinMode(motorkiri_arah_belakang, OUTPUT); 
    digitalWrite(enablePin, HIGH); 
}
void mundur_long()
{
      digitalWrite(motorkanan_arah_depan, LOW);  
      digitalWrite(motorkanan_arah_belakang, HIGH);  
      digitalWrite(motorkiri_arah_depan, LOW);   
      digitalWrite(motorkiri_arah_belakang, HIGH);  
      delay(500); 
}
void berhenti()
{
    digitalWrite(motorkanan_arah_depan, LOW);  
    digitalWrite(motorkanan_arah_belakang, LOW);
      
    digitalWrite(motorkiri_arah_depan, LOW);  
    digitalWrite(motorkiri_arah_belakang, LOW);
    delay(500);
}
void maju()
{
      unsigned int uS = sonar.ping(); 
     
      sensor1_val = uS / US_ROUNDTRIP_CM;
      Serial.println("\nsensor depan val : ");
      Serial.print(sensor1_val);
      Serial.print(" cm\n");
      if (sensor1_val < 50 && sensor1_val > 0) {
          mundur_long();
          avoid();
      }
      Serial.println("\nmaju\n");
      digitalWrite(motorkanan_arah_depan, HIGH);   //right
      digitalWrite(motorkanan_arah_belakang, LOW);
      digitalWrite(motorkiri_arah_depan, HIGH);   //left 
      digitalWrite(motorkiri_arah_belakang, LOW);
      delay(50);
       
}
void op()
{
    maju();  
}
void avoid()
{
       int loopback;
       unsigned int uSkiri = sonar_kiri.ping();
       unsigned int uSkanan = sonar_kanan.ping();
     
       sensorkiri_val = uSkiri / US_ROUNDTRIP_CM;
       sensorkanan_val = uSkanan / US_ROUNDTRIP_CM;
       Serial.println("if block executed");
       Serial.println("\nsensor kiri val : ");
       Serial.print(sensorkiri_val);
       Serial.print(" cm\n");
       Serial.println("\nsensor kanan val : ");
       Serial.print(sensorkanan_val);
       Serial.print(" cm\n");
       if (sensorkiri_val > 60) {
           Serial.println("belok kiri"); 
           berhenti();
           belok_kiri(); 
      } 
      else if (sensorkanan_val > 60){
           Serial.println("belok kanan"); 
           berhenti();
           belok_kanan(); 
      } 
      else {
           for (loopback = 0; loopback < 2; loopback++) {
               mundur_long(); 
           }
      }
}
void belok_kanan()
{
      digitalWrite(motorkanan_arah_depan, LOW);  
      digitalWrite(motorkanan_arah_belakang, HIGH);  
      digitalWrite(motorkiri_arah_depan, LOW);  
      digitalWrite(motorkiri_arah_belakang, LOW);
      delay(500); 
}
void belok_kiri()
{
      digitalWrite(motorkanan_arah_depan, HIGH);  
      digitalWrite(motorkanan_arah_belakang, LOW);  
      digitalWrite(motorkiri_arah_depan, LOW);   
      digitalWrite(motorkiri_arah_belakang, HIGH);
      delay(500); 
}
void loop() {
   op();
}

Once success, When the robot avoid front obstacle, it will then check left for another obstacle existence, if no obstable on left, it will then turn left. Otherwise, if there’s another obstable on left, it will then check for right side for obstable, if there’s an obstable, it will turn back, otherwise it will turn right.

Here’s the robot on the road: