Category Archives: Hardware Hacking

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

Introducing Haxtronic – A Robotic Brain That Can Scan for Wireless Network Around

Introducing Haxtronic – A Robotic Brain for Scanning Wireless Network Around, this robotic brain is powered by kali linux and Banana PI. This robotic brain can be connected to arduino as motoric using various way such as using ethernet, power level shifter, etc.

Actually this robot can use aircrack-ng already. Currently programming for visual functionality for this robot using opencv.