Electronics Champ
Published © GPL3+

Sound Controlled Car using Arduino and NPN Transistor

This project shows how to build a car that can be controlled by sound.

BeginnerShowcase (no instructions)1 hour800
Sound Controlled Car using Arduino and NPN Transistor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×2
Microphone, Omnidirectional
Microphone, Omnidirectional
×1
Through Hole Resistor, 3.3 kohm
Through Hole Resistor, 3.3 kohm
×1
Resistor 220 ohm
Resistor 220 ohm
×1
660 Ω Resistor
×1
LED (generic)
LED (generic)
×1
L298N Motor Driver Module
×1
DC Motor, 12 V
DC Motor, 12 V
×2
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic for Sound Sensor side

Schematic for Arduino side

Code

Code

Arduino
Upload this code to the Arduino board
//Initialize the variables
uint8_t rightMotorForward = 5;
uint8_t rightMotorBackward = 6;
uint8_t leftMotorForward = 10;
uint8_t leftMotorBackward = 11;
uint8_t sensor = A0;
byte clapCount = 0;
byte prevClapCount = clapCount;

void setup() {

  //set the modes of the pins as input/output
  for (int i = 9; i < 13; i = i + 1) {
    pinMode(i, OUTPUT);
  }

  pinMode(sensor, INPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);

  //set the speed of the motors
  analogWrite(A1, 128);
  analogWrite(A2, 128);

  //start serial communication
  Serial.begin(9600);

}

void loop() {

  //if a sound is detected...
  if (analogRead(sensor) < 50) {
    clapCount = (clapCount < 7) ? ++clapCount : 0; //updates the variable 'clapCount'
    delay(1000);
  }

  //if the data stored in 'clapCount' changes...
  if (prevClapCount != clapCount) {

    //moves the car accordingly
    switch (clapCount) {

      case 0:
        stop();
        break;
      
      case 1:
        front();
        break;

      case 2:
        stop();
        break;

      case 3:
        back();
        break;

      case 4:
        stop();
        break;

      case 5:
        left();
        break;

      case 6:
        stop();
        break;

      case 7:
        right();
        break;

    }

    prevClapCount = clapCount;
    
  }

}

//function to move the car forward
void front() {

  analogWrite(leftMotorForward, 128);
  analogWrite(rightMotorForward, 128);
  analogWrite(leftMotorBackward, 0);
  analogWrite(rightMotorBackward, 0);
  Serial.println("Front");

}

//function to move the car backwards
void back() {

  analogWrite(leftMotorForward, 0);
  analogWrite(rightMotorForward, 0);
  analogWrite(leftMotorBackward, 128);
  analogWrite(rightMotorBackward, 128);
  Serial.println("Back");

}

//function to turn the car left
void left() {

  analogWrite(leftMotorForward, 0);
  analogWrite(rightMotorForward, 128);
  analogWrite(leftMotorBackward, 0);
  analogWrite(rightMotorBackward, 0);
  Serial.println("Left");

}

//function to turn the car right
void right() {

  analogWrite(leftMotorForward, 128);
  analogWrite(rightMotorForward, 0);
  analogWrite(leftMotorBackward, 0);
  analogWrite(rightMotorBackward, 0);
  Serial.println("Right");

}

//function to stop the car
void stop() {

  digitalWrite(leftMotorForward, LOW);
  digitalWrite(rightMotorForward, LOW);
  digitalWrite(leftMotorBackward, LOW);
  digitalWrite(rightMotorBackward, LOW);
  Serial.println("Stop");

}

Credits

Electronics Champ
4 projects • 11 followers
Projects based on breadboard electronics and Arduino with clear step-by-step instructions, circuit diagrams, schematics, and source code.
Contact

Comments

Please log in or sign up to comment.