Here is a simple robotics project to undertake with friends or folks at your local maker space. You will need a 3D printer handy to 3D print the STL files attached below, a couple of DC motors with wheels and zip ties to put everything neatly together. And for the electronics, you will need an Arduino Nano, a motor driver , ultrasonic sensor , breadboard and bread boarding wire. There is enough space between the bottom and top 3D printed part to fit an Raspberry Pi , or Arduino Uno with a motor shield.
3D printing
Here is a picture of the 3D printed parts which are designed in Fusion 360. You can use PLA or ABS, and you will not require any support material while 3D printing. STLs files for the 3D printed parts attached below. Basically there are two parts, the bottom to hold the battery and the breadboard, and the yellow top cover to hold the ultrasonic sensor.
The motors are added to the bottom 3D printed parts using zip ties, which are much simpler to find compared to the exact screws and nuts..
Circuit
Here are the circuit connections using the Sparkfun motor driver(TB6612FNG) and Arduino Nano.
- PWMA on the motor driver connected to pin# 5
- AIN1 to pin# 4
- AIN2 to pin# 2
- PWMB to pin# 6
- BIN1 to pin# 7
- BIN2 to pin# 8
- STBY to pin# 3
And for the ultrasonic sensor(HC-SR04) connected to
- Trigger Pin to Arduino pin# 9
- Echo Pin to Arduino pin # 10
Upload the Code using Arduino IDE
Upload the code below using the Arduino IDE, and if you want to test the motors individually , un-comment section below that says - "//uncomment this section", and comment the if and else loop above it.
//Project14-Nanorama- Arduino Nano bot- using ultrasonic sensor
//Motor Driver Pin Connections to Arduino Nano
int STBY = 3; //standby
//Motor A -left
int PWMA = 5; //PWM pin for Speed control
int AIN1 = 4; //Direction
int AIN2 = 2; //Direction
//Motor B - right
int PWMB = 6; //PWM pin for Speed control
int BIN1 = 7; //Direction
int BIN2 = 8; //Direction
//Ultrasonic sensor pins numbers
int trigPin = 9; //PWM
int echoPin = 10; //PWM
long duration;
int distance;
//Change this value to adjust the senstivity of the HC-SR04
const int maxDistance = 15;
void setup() {
//Setting the trigPin as an Output
pinMode(trigPin, OUTPUT);
//Setting the echoPin as an Input
pinMode(echoPin, INPUT);
//Setup Motor pins
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(STBY, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
//Starts the serial communication to help debugging
Serial.begin(9600);
}
void loop() {
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
//Sets the trigPin to HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
//Calculating the distance
distance= duration*0.034/2;
//Prints the distance on the Serial Monitor- use this to modify the maxDistance above
Serial.print("Distance: ");
Serial.println(distance);
if(distance < maxDistance){ //obstacle detected in front, move back and then turn left
move(100,'B');
delay(100);
turn(150,'L');
delay(200);
}else{ //keep moving foward
move(100,'F');
}
delay(200);
//uncomment this section, and comment the if-else section above to test the direction of the motor spining
/*turn(200,'L');
delay(300);
move(220,'B');
delay(300);
turn(210,'R');
delay(300);
move(220,'F');
delay(300);
stop();
delay(600); */
}
//Driving the CupRobot functions
void turn(int speed,char side){
digitalWrite(STBY, HIGH);//enable the standby pin
if(side == 'L'){ //left turn
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
analogWrite(PWMA, speed);
digitalWrite(BIN1, LOW); //Stop motor B
digitalWrite(BIN2, LOW);
analogWrite(PWMB, speed);
}else{ //Right turn
digitalWrite(AIN1, LOW);//Stop motor A
digitalWrite(AIN2, LOW);
analogWrite(PWMA, speed);
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, LOW);
analogWrite(PWMB, speed);
}
}
void move(int speed,char moveDirection){
digitalWrite(STBY, HIGH);//enable the standby pin
if(moveDirection =='B'){
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
analogWrite(PWMA, speed);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, HIGH);
analogWrite(PWMB, speed);
}else{
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
analogWrite(PWMA, speed);
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, LOW);
analogWrite(PWMB, speed);
}
}
void stop(){
//disable standby
digitalWrite(STBY, LOW);
}
And, if you want to run a test using the ultrasonic sensor and the motors to set the maxDistance parameter use the serial monitor as shown below.
Add a 2 cell lipo battery to the bottom of the 3D printed part with zip ties as you see in the picture below. In my case I am using Turnigy nano-tech 460mah 2S , but a regular 9V battery will also fit the 3D printed part.
Add the ultrasonic sensor to the top 3D printed part as shown in the picture below, and you can also use zip ties in the front and back to semi permanently add the top to the bottom STL.
Here is a quick video demo







Top Comments