Rolly our first autonomous microbot now enhanced to do more.
You’ll need these:
- HC-SR04 – Get this ultrasonic sensor from eBay.com.au for $2.
- SG90 Servo Motor – We’ve seen this servo selling for around $4 on ebay.
- Motor drive shield – You can get this expansion board for $5 online.
If you’re new to APC we’ve been spending the last few months of this Masterclass looking at how to use the low-cost Arduino microcontroller in the real world. These little 8-bit controllers don’t have the horsepower of a Core i5 chip but they’re far easier to interconnect with ‘real world’ devices such as motors ultrasonic sensors and temperature/humidity monitors.
In the last project we took a big step forward in complexity by building our first autonomous robot called Rolly. Rather than a kit we designed and built it from scratch to show how you can combine multiple components together to create a more complex device – and maybe even inspire you to have a crack at it yourself.
In this second part of the project we’ll look at some theory behind the robot’s operation some more troubleshooting and a neat trick you can perform with your robot.
Theory & troubleshooting
While our robot is pretty easy to put together what’s happening inside the Arduino controller itself is mostly hidden behind some brilliant code libraries made available by the Arduino community. Although you don’t really need to know much about what’s going on it’s always well worth understanding a little of the theory behind the main devices we’re using and how they work and interact with the software libraries.
HC-SR04 ultrasonic sensor
The tiny little HC-SR04 ultrasonic sensor looks like a pair of eyes but what you have is an ultrasonic transmitter and receiver the transmitter sending out pulses at around 40kHz and the receiver waiting for the echo to come back similar to the way bats navigate in the dark.
With the electronics on the sensor itself taking care of the nuts and bolts of sending those pulses the Arduino only has to initiate the pulse and measure the time it takes for the echo to return. Surprisingly this $2 sensor is accurate down to 3mm and works over a range of up to 4.5m. The sensor has four pins – two for power and two for control. Operation is pretty simple – the Arduino pulls the ‘trigger’ pin high to give the sensor the go-ahead to send a ‘ranging’ pulse.
The Echo pin goes high at the same time and remains high until the sensor receives the echo back from an object in front of the sensor. The length of time the Echo pin is high is proportional to the distance to any object in front and the library code simply turns that time delay into a distance in centimetres.
The HC-SR04 an ultrasonic sensor for under $2 on eBay.
SG90 Servo motor
What makes the HC-SR04 more useful is anchoring it on top of a digitally controlled servo motor. Servo motors are a three-wire geared motor that usually travel through no more than a 180Âº arc but can be positioned with reasonable accuracy within that arc based on the data sent to the servo on the data pin. These servos are normally used on RC helicopters to control rotor function but we’re using it as a ‘neck’ for our ultrasonic sensor.
Again the hard work of coding is handled by the built-in ‘servo’ library and all we need to do is tell the servo motor the degree position we want and it moves there. However the complication arises in getting the ultrasonic sensor and the servo motor working in sync so we can create something of a distance map in front of our robot.
What our code does is move the servo motor a few degrees to its new position waits 90ms for the servo motor to reach that position triggers the sensor waits another 70ms to get the echo back moves the servo the same degree arc again to its new position triggers another measure and so on until it reaches the end of its arc spins the servo back to the beginning and does it all over again.This way we not only know what’s directly in front of the robot but what’s off to the left and right as well.
If you’ve chosen one of the low-cost SG90 model servo motors don’t force-turn it with the rotor arm attached. These are basic nylon plastic-geared servo motors – they’re strong enough to handle turning our ultrasonic sensor but won’t take much to strip those gears if you start trying to force things.
The SG90 micro servo motor weighs just 9g and has plastic gears.
Motor Drive Shield
While the Arduino controller can handle interfacing with LEDs servo motors and ultrasonic sensors what it can’t do is directly control standard DC motors. These motors are basically ‘dumb’ motors – they have just two connectors for power. You apply power the motor spins. Add more power the motor spins faster. Swap the power and the motor spins in the opposite direction. That’s about it. The problem is these DC motors can chew up considerably more current than the Arduino outputs can deliver particularly when the motor stalls (i.e. is stopped from spinning for say running up against a wall).
So we need a $5 Motor Drive Shield (‘shield’ is Arduino-speak for expansion board) to provide some heavy lifting. Putting it simply this expansion board has two L298D H-pack motor controller chips. H-pack refers to the way the controller operates – it uses two pairs of two MOSFETs (metal-oxide silicon field-effect transistors) with the motor as the ‘-‘ in the ‘H’ between the two pairs. By changing which pairs of MOSFETs conduct you can easily swap the voltage polarity on the motor and make it change direction.
Each L298D chip can control two motors and with two L298D chips on the board we can handle up to four DC motors.
We also connect the servo motor and the ultrasonic sensor to this board but only as a matter of convenience – these device connections are fed through to the Arduino which handles them directly.
The L298D chip is only really for small hobby-grade motors like we’re using here. But when you’re ready to get into some serious motor control there are far more powerful motor controllers available that still work with the Arduino.
The Motor Drive Shield (expansion board) handles the DC motors for our Arduino board.
That said we’ve had a small rethink about how the DC motors are wired up. In the first part of this project we originally suggested both motors be controlled by just one of the two L298D chips the one that delivers the M1 and M2 outputs. The reason we did this is because due to limitations in the Arduino these two outputs can handle a much higher frequency in their PWM (pulse-width modulation) control – up to 64kHz whereas outputs M3 and M4 are fixed at 1kHz. The idea is at 64kHz the motors use a little more current but don’t whine as much. In the end we needed to keep power consumption down so we stuck with a 1kHz PWM rate.
After some more time we’d recommend swapping the right DC motor from the M2 output to M4 and using that second L298D chip. That should spread around the heat generated and keep everything happy. You’ll need to modify the
APC_4_ROBOT.ino code by changing the line:
AF_DCMotor motor2(2 MOTOR12_1KHZ);
AF_DCMotor motor2(4 MOTOR12_1KHZ);
Our revised block diagram with the shifted right DC motor connections.
The other thing to watch out for is just to make sure you keep the ultrasonic sensor parallel to the ground. If it leans forward you may actually drop the distance between the sensor and the floor to less than its trigger distance. That may see the robot always trying to turn away from an imaginary object when in fact it’s just pointing too low. So keep it parallel to the ground (maybe even pointing upwards a bit) and it should be fine.
How to add live video capture to your robot
Using free apps a smartphone a tablet and your Wi-Fi network you can watch a live feed of what your robot sees.
With an autonomous robot that decides its own direction one of the cool things you can do pretty easily is to add a video camera and capture what your robot sees. You may have seen a few RC helicopters and drones that now come with built-in or snap-on video camera modules but for our robot you don’t have to go to any elaborate lengths – just use your smartphone!
All you need is a basic mounting bracket to slot your phone into and that’s pretty easy to make. We used a couple of 2 x 1 right-angle steel brackets from Bunnings – they cost 80c each and are perfect for the job. To make a simple U-channel slot we double-side-taped one to the top of the battery case and added a second bracket on top of the first facing the other way.
To make sure your phone stays somewhat in place just add another row of double-sided tape to the base on the slot so your phone will stay in position. Switch your phone on set it to ‘video’ mode and just manoeuvre it until you get a good shot of what’s in front (I like to get the sweeping action of the ultrasonic sensor in at the edge of the frame so you can see the phone is riding on the back of the robot).
Set your phone to ‘record’ switch on the robot and away you go. You can download the video to your computer and watch it on your screen or send it to your TV.
If you have a couple of Android phones or an Android phone and tablet plus a Wi-Fi network you can turn Rolly into a live-video-streaming robot and watch where he goes in real-time. What’s more you can do it all with free software.
First head to Google Play and download the free IP Webcam app to your smartphone. IP Webcam turns your smartphone into a video webcam and server that streams the video output over your wireless network.
On your second Android device download tinyCam Monitor FREE also from Google Play set it to view the output from your smartphone’s IP address and you’ll be able to view the output from your smartphone’s camera in real-time on the second device.
If you don’t have a second phone or tablet you can still use your PC or notebook – just point your web browser to the IP address of your phone (including the ‘:8080′ port address) and the IP Webcam web server will still allow you to see the action. At this stage we haven’t found an equivalent free iOS app for IP Webcam but you just need an app that does the same thing – turns the phone into an IP webcam and has its own web server built in.
It’s important you don’t do what I did and pick up your robot with the phone still attached to it. I was stupid enough to not have the double-sided tape on it the phone fell out hit the ground from about a metre and the screen shattered. So this is definitely ‘do what I say don’t do what I did’ territory.
Follow our step-by-step guide below and you should have it up and running in around half an hour. The thing to remember is the more ‘basic’ your robot smartphone the lower the video quality and slower the frame rate. But if you have an old smartphone lying around in a drawer this is a great way to give it something to do.
Install IP Webcam onto your smartphone launch it and it’ll automatically open up to ‘Settings’. Check that the ‘Resolution’ is on your phone’s maximum supported video resolution for best quality.
Assuming you already have your phone connected to your home Wi-Fi network scroll down and select ‘Start Server’.
IP Webcam app should now switch to ‘video capture’ mode and will display its IP address towards the bottom of the screen. Note this address – you’ll need it in a moment.
If you have your PC running you can check that the IP Webcam web server is running by loading up your web browser and pointing it to that IP address from step 2 (you must include ‘:8080′ in the address or it won’t work). Forget the sound – well at least at the same time as the vision. It’s about five seconds out of sync. But if you see an image from your phone it’s working.
Create a U-channel slot from a couple of stainless-steel right-angle brackets (Bunnings 80c each) using double-sided tape on top of the battery box. Place another piece inside the channel that the phone can rest on and be somewhat stable. Place your phone in the channel and align it so you can see a ‘driver’ view of your robot preferably with the ultrasonic sensor towards the side of the view.
Grab a second Android smartphone or tablet and install tinyCam Monitor FREE from Google Play. When it comes up cancel the pop-up questions and select ‘Manage Cameras’ from the main screen.
When asked if you’d like to add a new camera click ‘Yes’. On the ‘Camera Settings – Cam 1′ screen change the ‘Camera Vendor’ to ‘IP Webcam for Android’ and change the IP address to the one from step 2 (don’t worry about the ‘:8080′ here).
Press the ‘OK’ button at the top-right and use the back button to go back to the main screen but this time select ‘Live view’ choose your camera select ‘Switch Layout’ when you get the four-box view and your IP Webcam-powered smartphone should come into full-screen view. Set Rolly off and you can now watch where he goes in real time.
We’re returning to more classical endeavours in the next project looking more at pulse width modulation (PWM) and even though it was never designed to do it seeing if we can get our Arduino Uno making a few sounds.