Ultrasonic sensor HC-SR04 with Dagu Mini Driver on a Robot Chassis

I am working on a Raspberry Pi based robot, and recently acquired a Dagu chassis and a Dagu Mini Driver for this project. As soon as I had the mini driver hooked up and the wheels starts turning, I felt an irresistible urge 😉 to put an ultrasonic sensor on the chassis and have it roam around by itself.
I’ve written about hooking up the inexpensive and fun Ultrasonic sensor HC-SR04 with an Arduino before. The wiring is the same – instead of a real Ardunio board, we’re just using the Arduino compatible Dagu Mini driver here. You can see some details for this board at the link below. Unfortunately, they don’t give you the full schematics.
http://www.dagurobot.com/goods.php?id=142
As far as construction goes, the Dagu chassis is very easy to put together. It can, and in fact it was put together by a 9 year old. 🙂

Also as you can see, I’ve used very high tech Tie Wrap Technology to attach the sensor to the chassis. 😉
The motors and the board are powered by a 3000 mAh 1A Anker rechargeable battery – good stuff!
The programming part is quite simple. The main loop sends out a ping on the ultrasonic sensor. If it returns a distance less than a set threshold the wheels are made to stop and reverse. Then, the chassis is made to turn left by a bit, and this process repeats. This is a very simplistic algorithm, but you can modify it for more sophisticated behavior.
You can see the robot in action in the video below.
//---------------------------------------------------------- // dagu_ultrasonic.ino // // Simple test of ultrasonic sensor HC-SR04 connected to // Dagu mini driver (arduino compatible board) and a two // wheel Dagu chassis. // // Author: Mahesh Venkitachalam // Website: electronut.in //---------------------------------------------------------- int pinTrigger = 2; int pinEcho = 4; int pinLMDir = 7; int pinLMPWM = 9; int pinRMDir = 8; int pinRMPWM = 10; //---------------------------------------------------------- // send a ping from ultrasonic sensor HC-SR04 and return // distance in cm float ping() { // send a 10us+ pulse digitalWrite(pinTrigger, LOW); delayMicroseconds(20); digitalWrite(pinTrigger, HIGH); delayMicroseconds(10); digitalWrite(pinTrigger, LOW); delayMicroseconds(20); // read duration of echo int duration = pulseIn(pinEcho, HIGH); // dist = duration * speed of sound * 1/2 // dist in cm = duration in us * 1 x 10^{-6} * 340.26 * 100 * 1/2 // = 0.017*duration float dist = 0.017 * duration; return dist; } //---------------------------------------------------------- void setup() { // set up motor pins pinMode(pinLMDir, OUTPUT ); pinMode(pinLMPWM, OUTPUT ); pinMode(pinRMDir, OUTPUT ); pinMode(pinRMPWM, OUTPUT ); // set up sensor pins pinMode(pinTrigger, OUTPUT); pinMode(pinEcho, INPUT); } //---------------------------------------------------------- // move forward/bacwa void move(bool forward, int timeMS, int speedPercent) { // set speed int speedPWM = (255*speedPercent)/100; // set direction if(forward) { digitalWrite(pinLMDir, HIGH); digitalWrite(pinRMDir, LOW); } else { digitalWrite(pinLMDir, LOW); digitalWrite(pinRMDir, HIGH); } // set speed analogWrite(pinLMPWM, speedPWM); analogWrite(pinRMPWM, speedPWM); // sleep so it moves delay(timeMS); } //---------------------------------------------------------- // stop moving void stop() { analogWrite(pinLMPWM, 0); analogWrite(pinRMPWM, 0); } //---------------------------------------------------------- // turn void turn(bool left, int turnMS) { // set direction if(left) { digitalWrite(pinLMDir, LOW); digitalWrite(pinRMDir, LOW); } else { digitalWrite(pinLMDir, HIGH); digitalWrite(pinRMDir, HIGH); } // turn at 25 % speed analogWrite(pinLMPWM, 64); analogWrite(pinRMPWM, 64); // for x ms delay(turnMS); // stop analogWrite(pinLMPWM, 0); analogWrite(pinRMPWM, 0); // reset direction digitalWrite(pinLMDir, HIGH); digitalWrite(pinRMDir, LOW); } //---------------------------------------------------------- void loop() { // send a ping float dist = ping(); // is there an obstruction? if (dist > 0 && dist < 25) { // stop stop(); // move back move(false, 250, 60); // turn left turn(true, 100); } else { move(true, 500, 40); } delay(500); }
Not a complex project by a long shot. But it was a great fun detour for me, and I hope you find some useful information here.