element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
NanoRama
  • Challenges & Projects
  • Project14
  • NanoRama
  • More
  • Cancel
NanoRama
Blog Arduino Nano bot - using ultrasonic sensor
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join NanoRama to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: carmelito
  • Date Created: 14 May 2020 12:31 AM Date Created
  • Views 2640 views
  • Likes 6 likes
  • Comments 2 comments
  • nano
  • arduino nano
  • sensorsch
  • dc motors
  • nanoramach
  • ultrasonic proximity sensor
Related
Recommended

Arduino Nano bot - using ultrasonic sensor

carmelito
carmelito
14 May 2020

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.

image

 

 

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.

image

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..

image

 

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

image

 

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.

image

 

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.

 

image

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.

image

Here is a quick video demo

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Attachments:
NanoBot_STLfiles.zip
  • Sign in to reply

Top Comments

  • dubbie
    dubbie over 5 years ago +1
    Carmelito, A very nice little robot. I particularly liked the 3D printed shell, I might have to have a go at making something similar myself, it looks good. You could put the USB connection at the back…
  • carmelito
    carmelito over 5 years ago in reply to dubbie +1
    Yes, those are good ideas, i will try and add those to the next version of the 3D printed parts. A smiley/sad face in the front instead of the rectangular mouth will be a good look on the bot, I think…
  • carmelito
    carmelito over 5 years ago in reply to dubbie

    Yes, those are good ideas, i will try and add those to the next version of the 3D printed parts. A smiley/sad face in the front instead of the rectangular mouth will be a good look on the bot, I think I will need RGB leds/neopixels to achieve that .. Thanks for the suggestion - Carmelito

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • dubbie
    dubbie over 5 years ago

    Carmelito,

     

    A very nice little robot. I particularly liked the 3D printed shell, I might have to have a go at making something similar myself, it looks good. You could put the USB connection at the back and this would then leave some room for LEDs at the front to make maybe a smiley face or sad face as it encounters obstacles.

     

    Dubbie

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube