Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
TCSHAFER
Published © CC BY-ND

Arduino Autonomous Car!

A small car that can avoid obstacles and drive itself!

BeginnerProtip1,648
Arduino Autonomous Car!

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Jumper wires (generic)
Jumper wires (generic)
×22
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
LED (generic)
LED (generic)
×2
Servos (Tower Pro MG996R)
×2
Solid V Wheel Kit
OpenBuilds Solid V Wheel Kit
×2
Breadboard (generic)
Breadboard (generic)
×1
Slide Switch
Slide Switch
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Hot glue gun (generic)
Hot glue gun (generic)

Schematics

Wiring Diagram

How to wire your Arduino circuit board and Servo Motors!

Code

Autonomous Car Code

C/C++
#define trigPin 12
#define echoPin 13

int n; 
int duration, distance;

String readString;

#include <Servo.h> 
Servo myservo1;  // create servo object to control servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(8); // assigns the servo to a pin
  myservo2.attach(9);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(3, OUTPUT);
}

void loop() {
  digitalWrite(trigPin, HIGH);
  _delay_ms(500);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
        
  if (distance < 40) { // this is the distance which the rover has to stop
    digitalWrite(3, HIGH);
    myservo1.write(n); // controls the direction of the motors
    myservo2.write(180-n);
   delay(1000);  // how long the wheels spin
   myservo1.write(n);
   myservo2.write(90-n);
   delay(500);
}
  else { // what the rover will do if it doen't sense anything
    digitalWrite(3, LOW);
    myservo1.write(180-n);
    myservo2.write(n);

Credits

TCSHAFER

TCSHAFER

1 project • 1 follower

Comments