I’m just starting out building a four wheeled robot powered by Arduino. Initially, I’m hooking up four servos for driving, a distance sensor for obstacle avoidance, and an LCD display for debugging the distance sensor.
I’ll keep building on the project over time (maybe hook up GPS or IMU for navigation), and increasing the code complexity - right now the robot simply drives until it reaches an obstacle and then stops. I’ll also post a link to parts once I make some more progress.
Circuit:
- Cirkit file: four_wheeled_robot.json (16.8 KB)
Arduino UNO Code:
#include <Servo.h>
#include <LiquidCrystal.h>
// Servo variables
Servo servo_br; // back right servo
Servo servo_bl; // back left servo
Servo servo_fl; // front left servo
Servo servo_fr; // front right servo
int speedForward = 100;
int neutralSpeed = 1500;
int centeringOffset = -10;
// LCD Variables
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Ultrasonic sensor variables
const int trigPin = A0;
const int echoPin = A1;
void driveForward() {
servo_bl.writeMicroseconds(neutralSpeed + speedForward + centeringOffset);
servo_fl.writeMicroseconds(neutralSpeed + speedForward + centeringOffset);
servo_br.writeMicroseconds(neutralSpeed - speedForward + centeringOffset);
servo_fr.writeMicroseconds(neutralSpeed - speedForward + centeringOffset);
}
void driveStop() {
servo_bl.writeMicroseconds(neutralSpeed);
servo_fl.writeMicroseconds(neutralSpeed);
servo_br.writeMicroseconds(neutralSpeed);
servo_fr.writeMicroseconds(neutralSpeed);
}
void setup()
{
servo_br.attach(7);
servo_bl.attach(8);
servo_fl.attach(9);
servo_fr.attach(10);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
float duration = pulseIn(echoPin, HIGH);
float distance = (duration*.0343)/2;
// Print out the distance
// set the cursor to column 0, line 0
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
delay(100);
if (distance < 20) {
driveStop();
} else {
driveForward();
}
}