Tuesday, September 29, 2015

ARTICLE #6 : Using L293D dual H bridge motor driver with Arduino

Introduction :


The L293D H bridge is a simple circuit that allows the Arduino board to control two independent DC motors (also steppers and servos). So, for example in a robotics application, by connecting each DC motor to a robot’s wheel you will be able to program it to drive forward, backward and turn left or right. You can also control the rotation speed of the motors by connecting them to the PWM pins instead of the digital pins as we will see in the present tutorial.

L293D features and pin description :





The L293D H bridge is a device designed to control two DC motors from 4.5V to 36V simultaneously. It can provide bidirectional currents up to 600 mA. All its pins are TTL compatible and it can work in a temperature range from -65°C to 150°C.

You can find all electrical features and other technical informations by viewing the datasheet. 





pinout description diagram :







  • L239D has two pin groups, pins from 1 to 8 used for the right motor and pins from 9 to 15 for the left one.
  • Vss is the device’s working voltage.
  • Vs is the motors supply voltage, it varies from 6V to 12V depending on the motor type.





  • Enable pins are used to activate the pin groups of each side of the L293D. If Enable pin is low, the output pin will be at 0 always. If it is high, output depends on input. In this tutorial, enable pins are connected to logical level one from the start.
  • Input pins :

Inputs 1, 2, 3 and 4 are connected to digital I/O pins of the Arduino. They are the pins that are used in the program code to control the output pins.

  • Output pins :

Output 1   (Pin 3)     ------- >   Negative Terminal lead of Right Motor
Output 2   (Pin 6)     ------- >   Positive Terminal lead of Right Motor
Output 3   (Pin 10)   ------- >   Positive Terminal lead of Left Motor
Output 4   (Pin 14)   ------- >   Negative Terminal lead of Left Motor


INPUT
1
INPUT
2
INPUT
3
INPUT
4
OUTPUT
1
OUTPUT
2
OUTPUT
    3
OUTPUT
4
MOTOR
OUTPUTS
MOVEMENT
RIGHT
LEFT
LOW
HIGH
HIGH
LOW
0
Vss
Vss
0
Straight
Straight
Forward
LOW
HIGH
LOW
HIGH
0
Vss
0
Vss
Straight
Reverse
Turn left
HIGH
LOW
HIGH
LOW
Vss
0
Vss
0
Reverse
Straight
Turn right
HIGH
LOW
LOW
HIGH
Vss
0
0
Vss
Reverse
Reverse
Backward




Circuit wiring :


     Project parts :


In this project, you will need :


  • 1 breadboard.
  • 1 Arduino Uno.
  • 1 L293D H-bridge motor controller.
  • 2 DC motors.
  • 9V battery.
  • Wire connectors. 

     Wiring :


As seen in the image below, there are two DC motors connected to the for output pins of the L293D H-bridge.each of these outputs is controlled by an input pin as described in the previous table, and we connect all these input pins to the microcontroller's pins.

Enable1 and enable2 pins, as well as Vss pin are connected to the 5V supply voltage of Arduino. Vs (pin No 8) is connected to 9V battery that is used to supply the two DC motors.

The for remaining pins are connected to the ground.




Code :


This program below makes the DC motors to both turn in one sens to perform the driving forward behavior of a wheeled robot, then turn in the other sens to perform a backward movement. When motors turn in opposite senses it performs a turn left or right movement. And finally it stops when both motors stop turning.

To be able to control the turning speed of DC motors, you just have to connect the 4 input pins to PWM pins of the microcontroller. you should also use analogWrite function in place of digitalWrite.

In a future development of this tutorial, I will enhance the previous system by adding a potentiometer that enables the user to increase or decrease the speed of these DC motors. I will add a picture of this project as well as the code when I finish writing it. Stay tuned! 


// Use this code to test your motor with the Arduino board:


// ---------------------------------------------------------- Motors pin declaration
int motor_left[] = {11, 10};
int motor_right[] = {9, 6};
int delay_sec = 10000;


// ----------------------------------------------------------- Setup function
void setup() {
Serial.begin(9600);

// Initializing motors as outputs
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}

}

// ------------------------------------------------------------ Loop function
void loop() { 
  
drive_forward();
delay(delay_sec);
motor_stop();
Serial.println("1");

drive_backward();
delay(delay_sec);
motor_stop();
Serial.println("2");

turn_left();
delay(delay_sec);
motor_stop();
Serial.println("3");

turn_right();
delay(delay_sec);
motor_stop();
Serial.println("4"); 

motor_stop();
delay(delay_sec);
motor_stop();
Serial.println("5");
}

// ----------------------------------------------------------- Drive functions

void motor_stop(){
analogWrite(motor_left[0], LOW); 
analogWrite(motor_left[1], LOW); 

analogWrite(motor_right[0], LOW); 
analogWrite(motor_right[1], LOW);
delay(25);
}

void drive_forward(){
analogWrite(motor_left[0], HIGH); 
analogWrite(motor_left[1], LOW); 

analogWrite(motor_right[0], HIGH); 
analogWrite(motor_right[1], LOW); 
}

void drive_backward(){
analogWrite(motor_left[0], LOW); 
analogWrite(motor_left[1], HIGH); 

analogWrite(motor_right[0], LOW); 
analogWrite(motor_right[1], HIGH); 
}

void turn_left(){
analogWrite(motor_left[0], LOW); 
analogWrite(motor_left[1], HIGH); 

analogWrite(motor_right[0], HIGH); 
analogWrite(motor_right[1], LOW);
}

void turn_right(){
analogWrite(motor_left[0], HIGH); 
analogWrite(motor_left[1], LOW); 

analogWrite(motor_right[0], LOW); 
analogWrite(motor_right[1], HIGH); 
}

Sunday, September 13, 2015

ARTICLE #5 - Control an Arduino from MATLAB

Introduction :



MATLAB is a technical computing software and also a high level programming language widely used by engineers and scientists in many fields to perform mathematical computation, algorithm development, simulation, analysis and data visualization and also GUI application development.

In this tutorial we will learn how to interface Matlab with the Arduino board.it simply means that you will use Matlab software to communicate with your Arduino board without using the Arduino IDE. So you will directly write the commands on the Matlab command window, send them to the Arduino board via the USB cable wich will then execute these commands and send back the results.


We will use Matlab built-in specific Arduino commands to write a simple blinking LED program, then we will write another program that reads the surrounding temperature using a TMP36 sensor (you can click here to see my previousarticle about the TMP36 temperature sensor) and also we will build a simple GUI application that plots this temperature data in a 2D graph.



Project parts :

Software : 

  • MATLAB
  • MATLAB support package for Arduino

Hardware



  • Arduino Uno
  • 1 Breadboard
  • 1 Red LED
  • 1 TMP36 temperature sensor
  • Wire connectors


Interfacing steps :

To install the MATLAB and Simulink hardware support packages for Arduino in the 2014a version and later releases, you click on “Add-ons” in “Home” tab, choose “Get hardware support packages”. Then from the “Support Package installer” window you click on “Install from internet” and you click on “Next”, after that you search for “Arduino” in the scroll bar section and you click on it, check all the three install boxes of the packages and you click “Next”. This launches the support packages installation, you follow  the installation steps until it finishes.

After the support packages were downloaded, we will test if all of this works fine. Connect the Arduino board to your PC. Under the MATLAB command window, you create an Arduino object by typing the following command line :


a = arduino(‘comX’,‘uno’)

(Where X should be replaced by the correspondent COM port of your Arduino)

You should see the following text once you click “ENTER” button :


Attempting connection .............

Basic I/O Script detected !

Arduino successfully connected !


This object can now perform digital and analog I/O functions and utilize all the functions specified by the arduino.m library.  


Circuit wiring 1 : Blinking LED




Matlab code:





Circuit wiring 2 : plotting temperature with TMP36 sensor






CODE :






Temperature Plot :



Sunday, September 6, 2015

ARTICLE #4 - Using Servo Motors with Arduino

Introduction :


The aim of this project is to learn about servo motors and how to control them using Arduino microcontroller. Servo motors are a type of actuators that are very used in electromechanics, robotics and mechatronic applications because they are responsible for the moving part of these applications such as rotating a wheel, moving a mechanical arm etc. In our case we will see first how to configure and program a servo motor to rotate forwards and backwards, then we will add to this system a potentiometer that will allow us to control the servo motor. But now I will answer some basic questions that will help us to  better understand this device.  

  • How a servo motor works ?
A servo motor uses pulse width modulation (pwm) to convert the signal coming to its output pin into a rotating movement. We will use the Arduino Servo library’s basic functions to apply this concept

  • What is the difference between servo and DC motor ?
Unlike a DC motor that rotates continuously, a servo motor reacts to preset angle value between 0 and 180 degrees to move to it. That’s what makes it useful for robotics and mechatronic applications.

  •  What are the main applications of a servo motor ?
Because of its special concept of feedback loop that allows it to measure the position of its output shaft, a servo motor becomes widely used in different applications requiring precise position control such as using it to control a robotic arm to allow it to move to a precise angle, it used to control conveyor belts in production factories, we can also find servo motors in solar panels for solar tracking systems, printers, camera’s auto-focus option, antennas positioning and so many application varieties.


Circuit wiring :


    Project parts :


The parts needed for this project are :

  • 1 Arduino Uno
  • 1 breadboard
  • 1 Servo motor
  • Multiple wire connectors to connect all the parts together







    Servo pin configuration:

Servo motors have three wires: power, ground and signal. You can generally identify every one of them by their colors.

The power wire should be connected to the 5V pin of your Arduino, its color is red.
The ground wire should be connected to the GND pin on your Arduino, its color may be black or brown.
The signal wire is typically white, yellow or orange and should be connected to a PWM pin on your Arduino. You can identify a PWM pin by finding a ‘~’ sign next to it.



    Wiring 1 :


The circuit wiring of the project is very simple. You connect the servo pins to the breadboard and Arduino as you see in the image below. Notice that you have to connect the signal wire to a pwm pin in Arduino. PWM pins are 3, 5, 6, 9, 10, 11.  





    Code :

After compiling and loading the sketch below into the Arduino board. The servo motor shaft starts to rotate clockwise from 0° to 180° then counter clockwise from 180° to 0° in a half circle form. 

//includes all the functions used for the servo motor
#include <Servo.h>

Servo servoMotor;
int positionServo = 0;
int potPin = 3;
  

void setup() {

     servoMotor.attach(10);
     servoMotor.write(0);

}

void loop() {

    //this loop turns the servo shaft clockwise
    for(positionServo = 0; positionServo < 180; positionServo += 10)     {

         servoMotor.write(positionServo);
         delay(100);
      
    }

   //this loop turns the servo shaft counter-clockwise
   for(positionServo = 180; positionServo > 0; positionServo -= 10)    {

         servoMotor.write(positionServo);
         delay(100);

    }

}

  
     Wiring 2 :


This new circuit is similar to the one above, you have just to add a potentiometer. The servo is still attached as shown in the previous example. The potentiometer has also 3 pins, connect the top pin to 5V, the bottom pin to GND, the middle pin has to be attached to one of the analog inputs of the microcontroller as shown below.





     Code :

The code below controls the position of the servo motor with the potentiometer. as seen in the previous image, the user can determine the position of the servo by turning the potentiometer that will send the corresponding variable resistance to the to the analog input pin A3, wich the Arduino code interprets and converts it to a PWM pulse that drives the servo into the determined position angle.


#include <Servo.h>

Servo servoMotor;
int positionServo = 0;
int potPin = 3
  

void setup() {

     servoMotor.attach(10);
     servoMotor.write(0);

}

void loop() {
  
    //reads the potentiometer value
    float pot_val = analogRead(potPin);

         //the map function re-maps a value number from one range to another
    //the pot_val variable gives a number from 0 to 1023
    //this number range converts to the 0 to 180 rotation degree range of the servo
         positionServo = map(pot_val, 0, 1023, 0, 180);

    //limits the potentiometer value to be between 0 and 180 
    constrain(positionServo, 0, 180);
  
    //sends the positionServo value to the servo motor
    servoMoteur.write(positionServo);

    delay(200);

}