Category Archives: Arduino

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:

Motor DC Control (1 direction only) Using ULN2803 (Darlington Array Transistor)

by : Antonius (@sw0rdm4n)

http://www,ringlayer.net

Part List(s)
– 1 Ceramic Disc Capacitors 50V 100nF
– 1 Mini Breadboard
– 1 uln2803
– Arduino / any compatible board
– 1 Tamiya single Gearbox
– 2 Wheel for tamiya gearbox
– Jumper Wires
– 6 v battery holder
– 4 x 1,5v battery
– 1 dc motor (toy)
– Soldering iron and tin

ULN 20803 is a darlington array transistor with 8 channel(s) suitable to control devices up to 500mA , 50v, the specification can be downloaded from here http://www.adafruit.com/datasheets/ULN2803A.pdf.

Here in this example we will use uln2803 to control a motor dc that uses single gearbox, since uln2803 isn’t a h-bridge so we will only able to control it with single direction ( for h-bridge, we may use tlc5940 ).

Here’s uln2803 pins:

uln2803

In this example, we will be using pin 1 as pwm pulse receiver from arduino pin 7 output. and pin 9 will be for ground connect to arduino’s ground. How to locate where’s pin 1 and another ? We can see on uln2803 that it has a “U” sign. The “U” sign determines that pin 1 is located on the left of that “U” sign.

First Step
Plug and solder jumper wire and ceramic disc capacitor into motor dc as follow:

uln1

Next plug the dc motor into tamiya single gearbox and plug some wheels. Here comes it :

x

As if this is a small vehicle, but it has no steering capabilities.

Second Step

connect pin 1 on uln2803 with pin 7 on arduino and connect ground pin (9) on uln2803 to ground pin on arduino.

20141118_065032

Next, connect both jumper from motor dc to pin 18 and pin 10 on uln2803

correct1

Next, connect ground from battery to ground pin 9 on uln2803, plug it beside ground pin from arduino, and plug volt from battery to pin 10 on uln 2803, plug it besides motor dc jumper wire as follow:

correct2

Final step is to upload this code:

int motor = 7;
void setup() {                
   pinMode(motor, OUTPUT);   
}

void loop() {
   digitalWrite(motor, HIGH);
   delay(300);   
   digitalWrite(motor, LOW);
   delay(1000);   
}

Once everything done, we can move the motor each 1 second about 0,3 second as follow:

Running text pada LCD 16×2 (DFrobot LCD Keypad Shield) dengan Arduino

By : Antonius (sw0rdm4n)
http:///www.ringlayer.net

Untuk menghubungkan arduino dengan layar lcd 16×2, dapat digunakan library LiquidCrystal. Pada contoh kali ini :

- lcd rs pin terhubung ke digital pin 8
- lcd enable pin terhubung ke digital pin 9
- lcd d4 pin terhubung ke digital pin 4
- lcd d5 pin terhubung ke digital pin 5
- lcd d6 pin terhubung ke digital pin 6
- lcd d7 pin terhubung ke digital pin 7

Contoh di atas kita menggunakan lcd keypad shield dari dfrobot:

lcd1

Berikut ini adalah contoh kode yang digunakan untuk membuat running text pada lcd 16×2 :

/*
Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
  modified August 6 2014
 by Antonius  (@sw0rdm4n)
*/

#include 
/*
- lcd rs pin to digital pin 8
- lcd enable pin to digital pin 9
- lcd d4 pin to digital pin 4
- lcd d5 pin t digital pin 5
- lcd d6 pin to digital pin 6
- lcd d7 pin to digital pin 7

*/
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {
    lcd.begin(16, 1);
}

void loop() {
  int i;
  
  lcd.setCursor(0, 0);
  lcd.print("www.ringlayer.net - Open Source - Electronic - Robotic - Security");
  for (i = 0 ; i < 16; i ++) {
      lcd.scrollDisplayLeft(); 
      delay(400);
  } 
}

Pada kode di atas, deklarasi di mana pin2 arduino dan lcd terhubung ditetapkan dengan kode

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

Selanjutnya kita tentukan ukuran lcd yang akan digunakan adalah pixel width sebesar 16 dan 1 :

    lcd.begin(16, 1);

Pertama-tama cursor diset pada pixel dengan kolom 0 dan row 0 :

  lcd.setCursor(0, 0);

Selantjunya print tulisan yang akan discroll :

 lcd.print("www.ringlayer.net - Open Source - Electronic - Robotic - Security");

Selanjutnya dilakukan pengulangan yang akan menggeser lcd ke kiri sebesar 1 pixel tiap pengulangan dengan interval 400 micosecond :

   for (i = 0 ; i < 16; i ++) {
      lcd.scrollDisplayLeft(); 
      delay(400);
  } 

Hasil tampilan running text bisa dilihat pada video ini :

Arduino Master and Slave Using I2C

By : Antonius (sw0rdm4n)

http:///www.ringlayer.net

Fritzing

schematic

On more complex electronic or robotic application sometimes require more than 1 arduino as controller. Connection between 2 arduino where one act as a master and other act as a slave can be accomplished using i2c. On above schematic we connect each ground pin on each arduino. We use analog input 4 and 5 for i2c communication. Analog input 4 on master connects to analog input 4 on slave, meanwhile analog input 5 on master connects to analog input 5 on slave. For testing we connect slave to pc and we use 9 V battery for master to send command.

i2c

Arduino IDE Code

For master use this code:

/*
Simple master to send command
by : Antonius (sw0rdm4n)
www.ringlayer.net
*/
#include <Wire.h>
void setup()
{
   Wire.begin();
   Serial.begin(9600);
   Serial.println("Master Test Start");
}
void loop()
{
   Wire.beginTransmission(4);
   Serial.println("Sending command to slave");
   Wire.write('R');

Serial.println("Command Sent !"); Wire.endTransmission(); delay(2000); }

On Slave we use this code :

/*
Arduino slave test using i2c
by : Antonius (sw0rdm4n)
www.ringlayer.net
*/
#include <Wire.h>
void setup()
{
         Wire.begin(4);
         Serial.begin(9600);
         Wire.onReceive(receive);
}
void loop()
{
          delay(1000);
}
void receive(int X)
{
    char dataslave = Wire.read();

    Serial.println("\nGot command  :\n");
    Serial.println(dataslave);
}

Testing command received from master on slave (slave connected to pc) :

slave

n.b:

Actually i2c can also be use to connect arduino and raspberry pi, since it has different voltage we can use pull up resistors to prevent any electronic component to get burnt.

Thanks :

Indonesian Backtrack Team and Members ( www.indonesianbacktrack.or.id )

Cr0 Team (www.cr0security.com)

 

Traffic Light With Push Button Control

This is simple electronic project with prototyping (no solder requirement), we’ll be using arduino and a push button to control a traffic light (rgb led)

Fritzing

traffic_schematic

Hardware Requirements:

4 resistor 220 Ω , 1 resistor 10 k Ω, 1 breadboard, 3 led (red, green, blue), 1 arduino, 6 jumper  wires,  1 push button

Fritzing

traffic1

traffic2

traffic3

we use 220 ohm resistor for each led and we connect it to arduino using pwm pin 8, 9 and 10, connect 10 k ohm resistor to push button , red jumper wire for push button connected to 5v pin on arduino, meanwhite black jumper wire connected to pwm pin 7 on arduino. the last black jumper wire we connect to ground pin on arduino.

We use breadboard here to make it easy, we don’t need to solder each component.

Arduino IDE Code

trafik4

trafik5

Here’s  complete code :

/*
traffic light dengan saklar on off - made by Antonius (@Sw0rdm4n)
http://www.ringlayer.net
*/
int merah = 8;
int kuning = 9;
int hijau = 10;
int saklar = 7; 
int on = 0;
int i;
void setup() {
	pinMode(merah, OUTPUT); 
	pinMode(kuning, OUTPUT); 
	pinMode(hijau, OUTPUT); 
	pinMode(saklar, INPUT);
}
void _run()
{
      digitalWrite(hijau, HIGH);
      delay(4000);  
      digitalWrite(hijau, LOW);
}
void _slow()
{
      digitalWrite(kuning, HIGH);  
      delay(2000);
      digitalWrite(kuning, LOW);  
}
void _stop()
{
    digitalWrite(merah, HIGH);
    delay(4000);  
    digitalWrite(merah, LOW);
}

void _traffic_light()
{
    do
    {
      _run();
       on = digitalRead(saklar); 
       if (on == 0) {
         _traffic_down();
       }    
       else {  
              _slow();
              on = digitalRead(saklar);   
               if (on == 0) {  
                  _traffic_down(); 
               }
               else {
                   _stop();
                   on = digitalRead(saklar);     
                   if (on == 0) {  
                      _traffic_down(); 
                   }
                   else {
                          _slow();
                   } 
               }
       }        
       on = digitalRead(saklar);     
    } while(on == 1);
}
void _traffic_down()
{
      for (i = 8;i < 11; i++) {
           digitalWrite(i, LOW); 
      }
}
void loop(){
	on = digitalRead(saklar);     
        Serial.println(on);
        if (on == 1) {
            _traffic_light();          
	} else {
            _traffic_down();
	}
        delay(500);
}

On success we can have a traffic light led that we can control with push button.

Infrared Sensor to Detect Range and Kinect Actuator

Fritzing

rear_front

This is simple and basic guide to detect object using infrared sensor. Hardware requirements : – an arduino or any arduino compatible board ( here I use sduino because it has enough pins to control 2 motor servos) – an infrared sensor – jumper wires (any color doesn’t matter) – 2 motor servo as kinetic actuator (front leg and rear leg)

all

First, plug jumper wires to infrared sensor, here we use sharp infrared sensor. Actually jumper wires color doesn’t matter, but to make it easy, we’ll plug red for 5v, black for ground and yellow for analog input :

ir1

Here we plug red jumper wires to 5v pin on sduino, black jumper wires to ground pin (ground is minus), plug yellow jumper to A5 (analog input pin 5). Plug 2 Servos Next we’ll be plugin 2 motor servos directly to our board using jumper wires, Plug the first motor servo for front leg :

ir2

for analog input we’ll be using pin number 9, for voltage we use 3,3v pin (actually to control a motor servo we can also use 3,3 v pin beside using 5v pin), next we plug the last jumper wire for ground. After plug the first motor, we plug the second motor (rear leg) :

ir3

for analog input, we plug jumper wire to pin 0, for voltage we plug red jumper wire to 5v pin, and the last jumper wire, plug it to ground pin. Actually colors of jumper wire doesn’t matter as long as the arrangement of cable that comes from motor servo plugged correctly with the pins on arduino. Programming The final step is uploading a program to use ir sensor, here’s our code for arduino ide :

/*
simple kinetic actuator using infrared
made by : Antonius (sw0rdm4n)
twitter :  @sw0rdm4n
http://www.ringlayer.net - https://sw0rdm4n.wordpress.com - http://sw0rdm4n.blogspot.com - http://www.indonesianbacktrack.or.id
*/
#include <Servo.h>
int sensorval;
int pos_front_leg = 0;
int pos_rear_leg = 0;
Servo front_leg, rear_leg;
void setup()
{
              front_leg.attach(9);
              rear_leg.attach(0);
}
void loop()
{
               sensorval = analogRead(5);
               if ((sensorval > 200) && (sensorval <300)) {
                           if (pos_rear_leg == 0) {
                                 rear_leg.write(180);
                                 pos_rear_leg = 180;
                           }
                           else {
                                 rear_leg.write(0);
                                  pos_rear_leg = 0;
                           }
              }
              else if (sensorval > 300) {
                          if (pos_front_leg == 0) {
                                front_leg.write(180);
                                pos_front_leg = 180;
                          }
                          else {
                                front_leg.write(0);
                                pos_front_leg = 0;
                          }
              }
              delay(500);
}

Upload that code to arduino board. To test we’ll be using 6 x 1,5 v battery :

ir4

On successfull, if we put any object near the infrared at sensor val range 201-299 the rear leg servo will be moving, if we put any object at sensor val range above 300 the front leg servo will be moving.