Posts Tagged ‘Shield’
Tweetbot … the code
You can now find the code of the twitter robot on my github at: https://github.com/pchretien/tweetbot Feel free to fork … copying is not stealing! :)
Arduino Motor Controller Using an L293D Chip
Controlling DC motors is at the heart of many robotic projects. Servo motors are sexy but DC motors are cheap and a lot more useful to control wheel based robots. In this article I’ll display a “robot shield” circuit that allows you to use cheap motors to drive your robot.
Wiring
This shield has been built on top of the Adafruit Prototype Shield. Using the prototype shield makes it much simpler to develop a custom Arduino shield. You can get one from Adafruit here.
The assembly is powered by a 6V battery connected to the Vin pin of the Arduino and to the voltage regulator input pin. The voltage regulator output is connected to pin 9 (Vcc2) of the chip to power the motors. The Arduino 5V pin is connected to pin 16 of the controller to power the internal logic. Make sure to use a proper heat sink on the voltage regulator and on the chip (pins 4,5,12,13) if you plan to power the robot with a higher voltage source.
The L293 current driver chip amplifies the input signal received on pins 2, 7, 10 and 15 to outputs pins 3, 6, 11 and 14. This allows us to take a low power signal from our circuit and transform it into a higher power signal for the motors. Using these 4 input/output the L293 can drive two motors forward and backward.
Motor #1 is connected to pins 3 and 6 and motor #2 is connected to pins 11 and 14. To drive motor #1 you apply 5V on pin #2 and 0V on pin #7. To drive the same motor in opposite directions you apply 5V on pin #7 and 0V on pin #2. You can use the same technique to drive motor #2 with pins 10 and 15.
Speed can be controlled by sending a logical pulse to the L293 “enable input” pins. Pin #1 drives the speed of motor #1 and pin #9 of motor #2. The best way to do this is by using the Arduino PWM outputs on pins 9, 10 and 11. In this example, the Arduino pin #10 is connected to the L293 pin #1 and the Arduino pin #11 is connected to the L293 pin #9.
Speed And Direction
There are two methods to control the direction of a wheeled robot. One is to build a direction mechanism similar to cars where the wheels are oriented in the direction you want the robot to go. This method involves complex mechanisms and requires more space for the robot to maneuver.
The alternate method is to drive left and right wheels in opposite directions allowing the robot to make a sharp 360 degrees turn. This method requires no additional mechanism and is much simpler to implement by using a micro-controller. However, this method assumes that both motors are running at the same speed, which is often not the case with cheap motors.
This code shows how to control direction using the L293 chip with an Arduino board.
...
void backward()
{
digitalWrite(MOTOR_1A, LOW);
digitalWrite(MOTOR_1B, HIGH);
digitalWrite(MOTOR_2A, LOW);
digitalWrite(MOTOR_2B, HIGH);
checkDistance = 1;
}
void forward()
{
digitalWrite(MOTOR_1A, HIGH);
digitalWrite(MOTOR_1B, LOW);
digitalWrite(MOTOR_2A, HIGH);
digitalWrite(MOTOR_2B, LOW);
checkDistance = 0;
}
void right()
{
digitalWrite(MOTOR_1A, LOW);
digitalWrite(MOTOR_1B, HIGH);
digitalWrite(MOTOR_2A, HIGH);
digitalWrite(MOTOR_2B, LOW);
checkDistance = 0;
}
void left()
{
digitalWrite(MOTOR_1A, HIGH);
digitalWrite(MOTOR_1B, LOW);
digitalWrite(MOTOR_2A, LOW);
digitalWrite(MOTOR_2B, HIGH);
checkDistance = 0;
}
void stop()
{
digitalWrite(MOTOR_1A, LOW);
digitalWrite(MOTOR_1B, LOW);
digitalWrite(MOTOR_2A, LOW);
digitalWrite(MOTOR_2B, LOW);
checkDistance = 0;
}
...
For example, the Tamiya double gearbox includes two low cost DC motors. The gearbox is amazing for small robotic projects but heading straight with these motors can be much more difficult than expected.
This is where the L293 chip comes to the rescue. This chip can control both the direction and the speed of the motors. To control the speed of the motors you connect the two PWM digital outputs of the Arduino micro-controller to the enable input pins 1 and 9 of the chip. As explained in the datasheet: “When an enable input is high, the associated drivers are enabled and their outputs are active and in phase with their inputs”.
You are now able to adjust the motor speeds by changing the PWM output value of the enable input of the faster motor. PWM outputs range from 0 to 255 so, to slow down a motor by a ratio of 10% you should set the associated PWM output to 230. Trial an error is the only way to get that done and make your robot head in a straight line.
void setup()
{
...
pinMode(10, OUTPUT); // To L293 pin #1
pinMode(11, OUTPUT); // To L293 pin #9
analogWrite(10, 185); // Faster motor at 72% of its full speed
analogWrite(11, 255); // Slower motor at 100% of its full speed
...
}
What’s Next
In a future article we will see how to use the shield to drive a simple Robot controlled by an infrared remote control. This project will pack together many tutorials published on this blog. If you have worked with the L293 chip to control motors, let us know in the comment section below.
Wireless Arduino/XBee Remote Control with Compass and Accelerometer
A few weeks ago we played with XBee wireless devices. Last week we played with accelerometers and compass. This week we will put it all together in a custom remote control. Our first project will be to remotely control a servo motor by using two Arduinos and two XBee modules.
The remote control (transmitter) should be straight forward to build using our last project and replacing the USB interface with the XBee module.
The wireless servo controller (receiver) on the other hand needs to be built from scratch. We will connect an XBee shield and a servo motor to an Arduino board. The Arduino receives the commands from the serial port and sets the servo position accordingly.
Remote Control (ransmitter)
Let’s start with the transmitter. Instead of using a simplified Arduino board as in our previous articles, we will be using a standard Arduino board, an XBee shield and a Proto Shield all stacked together. The prototyping board is used to make the circuit for the accelerometer and the compass sensors.
Both sensors are powered with 3.3v from the Arduino. The accelerometer X, Y and Z axis are respectively connected to the Arduino analog pins #0, #1 and #2. The compass SDA and SCL outputs for the IC2 protocol are connected to the Arduino analog pins #4 and #5.
The remote control is powered by a 6V NiMH rechargeable battery pack. The battery is connected to the Vin and Gnd beaders of the Arduino board.
Following is the program of the remote control used to read the sensors and send data to the receiver:
#include <Wire.h>
int x, y, z;
int pin13 = LOW;
int slaveAddress;
byte headingData[2];
int i, headingValue;
int HMC6352Address = 0x42;
void setup()
{
Wire.begin();
slaveAddress = HMC6352Address >> 1;
pinMode(13, OUTPUT);
Serial.begin(19200);
}
void loop()
{
// Send a "A" command to the HMC6352
Wire.beginTransmission(slaveAddress);
Wire.send("A"); // The "Get Data" command
Wire.endTransmission();
delay(10);
i = 0;
Wire.requestFrom(slaveAddress, 2); // Request the 2 byte heading (MSB comes first)
while(Wire.available() && i < 2)
{
headingData[i] = Wire.receive();
i++;
}
headingValue = headingData[0]*256 + headingData[1];
Serial.print("<");
Serial.print(int (headingValue / 10));
Serial.print(":");
x = analogRead(0);
y = analogRead(1);
z = analogRead(2);
Serial.print(x, DEC);
Serial.print(":");
Serial.print(y, DEC);
Serial.print(":");
Serial.print(z, DEC);
Serial.print(">");
delay(190);
}
Wireless Servo Controller (receiver)
To build the receiver you will need both an Arduino board and an XBee shield along with a servo motor. Connect the XBShield onto the Arduino board and the servo motor data wire to the Arduino pin #10. The pins #9, #10 and #11 of the Arduino are Pulse With Modulation (PWM) outputs. A pulse can be sent to these outputs to control a servo motor.
I have used a 9V battery to power the receiver but I would strongly recommend a rechargeable battery pack. The 9V battery will not last a long time driving the servo motor.
Following is the code of the receiver:
#include <Servo.h>
#define BUFFER_MAX 24
#define CENTER_MIN 490
#define CENTER_MAX 510
#define MAX_MAX 590
#define MON_MIN 410
Servo servoA;
byte counter = 0;
char buffer[BUFFER_MAX];
void setup() {
pinMode(13, OUTPUT);
Serial.begin(19200);
servoA.attach(10);
}
void loop() {
if (Serial.available()) {
char c = (char) Serial.read();
if(c == '<')
{
digitalWrite(13,HIGH);
counter = 0;
memset( buffer, 0, BUFFER_MAX);
}
else if(c == '>')
{
digitalWrite(13,LOW);
processMessage();
}
else
{
if(counter == BUFFER_MAX)
counter = 0;
buffer[counter++] = c;
}
delay(10);
}
}
void processMessage()
{
// Serial.println(buffer);
// Serial.println(counter, DEC);
int count = 0;
int tokenCount = 0;
char tokenValue[4];
int tokenValues[4];
for(int i=0; i<=counter; i++)
{
if(buffer[i] == ':' || i == counter)
{
tokenValues[tokenCount++] = atoi(tokenValue);
count = 0;
memset(tokenValue, 0, 4);
}
else
{
tokenValue[count++] = buffer[i];
}
}
if( tokenValues[3] > MAX_MAX )
{
int servoValue = ((int)tokenValues[0]) - 180;
if(servoValue < 0)
servoValue *= -1;
servoA.write(servoValue);
}
else
{
int angle = 90;
if(tokenValues[2] < CENTER_MIN)
angle = 180 - (tokenValues[2] - 400);
if(tokenValues[2] > CENTER_MAX)
angle = 600 - tokenValues[2];
if(angle > 180)
angle = 180;
if(angle < 0)
angle = 0;
servoA.write(angle);
}
}
XBee Configuration
To configure the XBShield you can refer to the ladyada website. First when using the XBShield, make sure to set the on-board switch to the right position at the right time. Then, when uploading a sketch to the board, place the switch on the DLINE position. And lastly, when using the XBee as serial port, set the switch to UART.
Netduino Plus (Part 1)
I just received my Netduino Plus. Netduino is an open source microcontroller development board working with the Microsoft .Net Micro Framework.
The good news it that the pins layout of the Netduino is compatible with the pinout of the Arduino, which means you can re-use your Arduino shields.
The Netduino Plus comes with a microSD slot and Ethernet support. It’s great to get these features without having to stack a shield on top of the board.
All I have done so far is blinking a LED on pin #13 … I’ll keep you posted when I get something more interesting to show.





