Build an Arduino Robot

Build an Arduino Robot

There is nothing like seeing a program you wrote interact with the physical world. For this lab experiment you can make and program your own robot. An Arduino robot.

I began with instructions for the micro robot from Greece but realized my soldering skills are lacking.

Greek Mini Robot

But it did give me an idea for the parts I would need to get a small form factor robot up and running. This led to a search of Amazon for applicable parts. I needed at least two motors, some model of Arduino, a L293D H-Bridge Motor Driver chip, some servos for additional capabilities and a platform for all the parts to attach to.

Here is what I ended up with.

Total cost was around $40.00.

Official Arduino Nano

I picked the nearly compatible generic Arduino Nano because of its built in USB support and small form factor and its price. I say nearly compatible because its USB driver chip is a cheaper version than what comes on an official Arduino. It requires special drivers that have to be installed on the computer you are using to program the Arduino. I used the article, How to Use Cheap Chinese Arduinos with the CH340G USB Chip On Yosemite and Windows. It will show how to install the driver and enable it so it works with the Arduino IDE where you can write your code.

 

My Cheap Arduino

To find the computer chip you would like to use consult this chart at the Arduino website.

To make sure things were working I connected the Nano to my computer, started up the Arduino IDE and loaded the blink example code or what they call a sketch. Then I uploaded it to the Arduino and watched the onboard LED blink on and off.

The next step, the exciting part, was to wire up the L293D chip to the Arduino.

RobotPlatform.com had a great explanation and pin diagrams of the L293D. But I used a video to really nail down how to attach the two parts together using two breadboards and jumper wires between the two.

In the video he maps out what pins to attach to on the Arduino. The pin numbers are written on the Arduino board.

But here is a diagram that I made that will help you line up the wiring pin by pin. First use the mini bread boards.

Looks like this when finished

I used one for the L293D motor driver chip and another for the Nano. You can use the jumper wires to push into the holes on the breadboard that line up with the pin you want to connect. You will need to attach more than one wire to some of the pins. This configuration will allow the battery pack to power the motors and the chip. Make sure you are supplying at least 5 volts. The VIN pin is for unregulated power so if your motors need more power just supply more voltage. The Nano has a built in voltage regulator.

  • Pin 8 -> VIN on Nano
  • Pin 1 -> Pin 16
  • Pin 9 -> Pin 16
  • Pin 16 -> 5v on Nano for power
  • Pin 4 -> Pin 5 for Grounding
  • Pin 12 -> Pin 13 for Grounding
  • Pin 4 -> Pin 13 for Grounding
  • Pin 12 -> Gnd on Nano
  • Pin 10 -> D5 on Nano controls right motor
  • Pin 15 -> D4 on Nano controls right motor
  • Pin 7 -> D7 on Nano controls left motor
  • Pin 2 -> D6 on Nano controls left motor
  • Pin 3 -> – on left motor
  • Pin 6 -> + on left motor
  • Pin 14 -> – on right motor
  • Pin 11 -> + on right motor
    • on battery pack -> VIN on Nano VIN is for raw power.
    • on battery pack -> GND on Nano

One way to wire it up

 

At some point after I had it all working I crossed the V5 wire with the ground and shorted out the Arduino. So be careful with that. I ended up finding a lower cost option and purchased three new Nanos. This way of wiring it up allows the Arduino and the motors to be powered from the same battery pack. I used the battery container that came with the chassis kit which supplies 6v.

Now it’s time to program the Nano and see what it can do. I used the code samples from the Arduino forum and modified it to work with the pins I selected for controlling the motors.

To program the Arduino get the IDE from Arduino.cc and install it. Follow the directions at How to use cheap Arduinos. In the IDE select the Tools menu and select the Board you have (in my case Nano), the processor (I have the ATamega328) and the port (mine shows as dev/cu.wchusbserial1420).

 

// Motor Controls
const int PWMELPin = 10; // Enable PWM Left Motor
const int PWMERPin = 11; // Enable PWM Right Motor
const int L1Pin = 6; // L1 = Left Motor Lead 1
const int L2Pin = 7; // L2 = Left Motor Lead 2
const int L3Pin = 4; // L3 = Right Motor Lead 1
const int L4Pin = 5; // L4 = Right Motor Lead 2
const int LEDPin = 13; // Telltale LED
const int Duration = 2000;
void setup()
{
   pinMode(PWMELPin, OUTPUT);
   pinMode(PWMERPin, OUTPUT);
   pinMode(L1Pin, OUTPUT);
   pinMode(L2Pin, OUTPUT);
   pinMode(L3Pin, OUTPUT);
   pinMode(L4Pin, OUTPUT);
   pinMode(LEDPin, OUTPUT);
   delay(10000);
}
//Main Loop
void loop()
{
   MotorL(1, 1);
   MotorR(1, 1);
   delay(3000);
   digitalWrite(LEDPin, HIGH);
   MotorL(0, 1);
   MotorR(0, 1);
   delay(200);
   MotorL(1, 1);
   MotorR(0, 1);
   delay(100);
}
// ***********************************************************************
// Motor Driver Routines**************************************************
// ***********************************************************************
// Function(Direction, Speed)
// Direction
// 1=Forward
// 0= Reverse
// Speed
// 1 = High/250
// 0 = Low/20
// ***********************************************************************
void MotorL(int Direction, int Speed)
{
// Left Motor
if (Speed == 1)
{
   analogWrite(PWMELPin, 250);
}
else
{
   analogWrite(PWMELPin, 20);
}
if (Direction == 1)
{
   digitalWrite(L1Pin, HIGH);
   digitalWrite(L2Pin, LOW);
}
else
{
   digitalWrite(L1Pin, LOW);
   digitalWrite(L2Pin, HIGH);
}
}
// ***********************************************************************
void MotorR(int Direction, int Speed)
{
// Right Motor
if (Speed == 1)
{
   analogWrite(PWMERPin, 250);
}
else
{
   analogWrite(PWMERPin, 20);
}
if (Direction == 1)
{
   digitalWrite(L3Pin, HIGH);
   digitalWrite(L4Pin, LOW);
}
else
{
   digitalWrite(L3Pin, LOW);
   digitalWrite(L4Pin, HIGH);
}
}
// ***********************************************************************

Upload this to your Arduino and see what happens. It should drive forward or backward for 3 seconds depending on which direction you attached the wires then it should stop turn quickly to the left and then go forward again. It will loop forever until you press the reset button or kill the power.

Let me know here how it works for you and follow the blog and twitter volumeint for future lab projects. Upcoming posts include IoT, Internet of Things and adding sensors to the Arduino robot.

Volume Integration Sponsors Education with Robots

Volume Integration Sponsors Education with Robots

We humans are fascinated with robots since at least the 10th century BC when Yan Shi is recorded to have created a human like automaton. Now we teach about robotics beginning in elementary school. Two popular technologies in our area are the First Lego League and the VEX Robotics Competition

Marsteller Robotics

The competition consists of moving objects into a goal with 15 seconds of autonomous operation followed by a 1 minute and 45 seconds of remote control driving. In addition the robots are paired up with a second robot to work as a team.

This process teaches the competitors social and teamwork skills in the process of building something that requires them to learn and exercise math, science, engineering and technology (STEM) skills.

Software

The robots are programmed in a language specifically created for education and robots called RobotC. It is an altered version of C. This sample from the programmer, Elizabeth, on team 14g from last year shows the autonomous sequence.

#pragma config(Motor, port7, claw, tmotorVex393_MC29, openLoop)
void clawopen (int time)
{
motor[claw]=80;
wait1Msec(time);
motor[claw]=0;
}
task autonomous()
{
if(SensorValue(button1)==1)
{
driveforward (400);
clawclose(700);
armup (1600);
drivebackwards(890);
turnleft(400);
driveforward(500);
armdown(2200);
wait1Msec(500);
clawopen (1000);
drivebackwards(1000);
}
else
{
swingleft(200);//blue
clawclose(700);
armup (1600);
drivebackwards(800);
turnright(550);
driveforward(500);
armdown(2200);
wait1Msec(500);
clawopen (1000);
drivebackwards(1000);
}
}
view rawautonomous.c hosted with ❤ by GitHub

A series of functions that the team programmer Elizabeth wrote, such as clawopen, armup and turnright are executed. These functions call internal robot functions that control motors and read from sensors. This snip of code shows the clawopen function which references the claw motor defined in the pragma at the top. The config ties the port number on the cortex to a specific motor type.

Hardware

The VEX competition requires the use of standard parts so teams are working from the same base. The brain of the robot is an item they call the cortex. It is an ARM processor with ports for motor and sensor connections. There is a joystick to control the robot and then there are sensor and motor modules. In addition there are collections of gears, wheels, belts and structural pieces to build the body.

Optical Shaft encoder to calculate distance traveled

Vex Motor 393

 

Teamwork

Each robot is built by a team of four to six students working together to design, build and program it. They also must work together during the competitions in determining strategy and managing their time as they sometimes need to make adjustments between matches.

Between matches they compete in individual events where they are judged on programming skills and robot driving skills.

Robotics Marsteller

During the preliminaries they are teamed with another robot team. So all the matches are two against two. They learn to problem solve under pressure and learn to work with the strengths and weaknesses of the random parings over many matches.

In the semi-finals of the tournament their math skills are tested as they determine the best teams to align with during the semi-finals. The top teams pick two other teams they want to compete with in the finals. It is a loud dash as teams negotiate and jockey for positions.

In the end there are multiple winners. They select the most inventive robot, the tournament champions, the second place team, programming skills winner, robot skills winner, design (quality of their engineering notebook) and judges award.

The best teams during the season go to the Virginia State competition and then the top teams go to a world championship.

Volume Integration and Volume Labs is proud to sponsor this fun and educational technological sport. Be sure and check back for updates on the progress of the Marsteller Team.