Arduino SA
  • Learn @ Arduino SA
  • Arduino Introduction
  • Support
    • Setup your Computer
    • Adding Libraries
    • Setting up a ESP32
  • Arduino Robot Car
    • Arduino Bluetooth Car
      • What you will need
      • Circuit Diagram
      • Assembly the Car
      • Upload the Code
      • Bluetooth setup and Test
    • 4WD Obstacle Avoidance
      • Introduction
      • Component List
      • Assembly
      • Component Description
      • Circuit Diagram
      • Computer Setup
      • Line Tracking Mode
      • Obstacle Avoidance Mode
  • Arduino Kits
    • Smart Plant Watering Kit
      • What you will need
      • Wiring Diagram
      • The Code
      • Pics
    • Basic Starter Kit
      • Component View
      • Installing the IDE
      • Add Libraries
      • Blink
      • Button Control LED Delay Switch
      • Active Buzzer
      • Passive Buzzer
      • Servo
      • IR Remote and Receiver
      • Stepper Motor
      • Eight LED with 74HC595
      • LCD
      • Digital tube with Traffic Light Experiment
      • Four digit tube display
      • LM35D Temperature Sensor
      • LDR 5516 experiment
      • Flame Sensor
      • 8 * 8 LED Module
      • Controlling Stepper Motor With Remote
    • Super Starter Kit
      • Tutorials
    • Best Beginner Kit for Arduino
      • Tutorials
    • Uno R3 RFID Kit
    • WIFI ESP32 IOT Kit
    • Arduino School STEM Kit
    • Arduino Ultimate Starter Kit
  • Blocking Coding Lessons
    • Beginner Lessons
      • Setting up mBlock
      • Blinking an LED
      • LED Switching
      • LED Chasing
      • Traffic Signal
      • Buzzer
      • Buzzer + Push Button
      • LED + Push Button
  • Sensor Kits
    • 37-in-1 Sensor Kit
      • Joystick
      • Relay
      • Big Sound
      • Small Sound
      • Tracking
      • Avoid
      • Flame
      • Linear Hall Sensor
      • Touch
      • Digital Temperature
      • Buzzer
      • Passive Buzzer
      • RGB LED
      • SMD RGB
      • Two-Color 5mm
      • Two Color 3mm
      • Reed Switch
      • Mini Reed
      • Heartbeat
      • 7 color flash
      • Laser emitter
      • PCB mounted push Button
      • Shock switch
      • Rotary encoder
      • Light Cup
      • Tilt Switch
      • Rolling ball tilt switch
      • Photoresistor
      • Temp and Humidity
      • Analog Hall
      • Hall Magnetic
      • Temp
      • Analog Temp
      • IR Emission
      • IR Receiver
      • Tap Module
      • Light blocking
Powered by GitBook
On this page

Was this helpful?

  1. Arduino Robot Car
  2. Arduino Bluetooth Car

Upload the Code

Code upload

Open up your Arduino IDE.

Connect your USB cable to your PC and Arduino board.

Please ensure that the TX and RX for the Bluetooth module is not connected. If this is connected then the code upload will fail.

Copy the code below to a new Arduino project and Click the upload button

// Motor A (Right) connections
#define ENA 9       // Enable/speed motor Right wheels
#define IN_1  8    // Right wheels
#define IN_2  7    // Right wheels

// Motor B (Left) connections
#define ENB 3       // Enable/speed motor Left Right
#define IN_3  5    		// Left wheels
#define IN_4  4    		// Left wheels

int command; 			      //Int to store app command state.
int speedCar = 255; 		// 50 - 255.

void setup() {

  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN_1, OUTPUT);
  pinMode(IN_2, OUTPUT);
  pinMode(IN_3, OUTPUT);
  pinMode(IN_4, OUTPUT);

  Serial.begin(9600);

}

void goAhead() {

  digitalWrite(IN_1, HIGH);
  digitalWrite(IN_2, LOW);
  analogWrite(ENA, speedCar);

  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, HIGH);
  analogWrite(ENB, speedCar);
}

void goBack() {

  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, HIGH);
  analogWrite(ENA, speedCar);


  digitalWrite(IN_3, HIGH);
  digitalWrite(IN_4, LOW);
  analogWrite(ENB, speedCar);
}

void goRight() {

  digitalWrite(IN_1, HIGH);
  digitalWrite(IN_2, LOW);
  analogWrite(ENA, speedCar);


  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, LOW);
  analogWrite(ENB, speedCar);
}

void goLeft() {

  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, LOW);
  analogWrite(ENA, speedCar);

  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, HIGH);
  analogWrite(ENB, speedCar);

}

void stopRobot() {

  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, LOW);
  analogWrite(ENA, speedCar);


  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, LOW);
  analogWrite(ENB, speedCar);
}

void loop() {

  if (Serial.available() > 0) {
    command = Serial.read();
    stopRobot(); 			//Initialize with motors stopped.

    switch (command) {
      case 'F': goAhead(); break;
      case 'B': goBack(); break;
      case 'L': goLeft(); break;
      case 'R': goRight(); break;
      case '0': speedCar = 100; break;
      case '1': speedCar = 115; break;
      case '2': speedCar = 130; break;
      case '3': speedCar = 145; break;
      case '4': speedCar = 160; break;
      case '5': speedCar = 175; break;
      case '6': speedCar = 190; break;
      case '7': speedCar = 205; break;
      case '8': speedCar = 220; break;
      case '9': speedCar = 235; break;
      case 'q': speedCar = 255; break;

    }
  }
}

PreviousAssembly the CarNextBluetooth setup and Test

Last updated 3 years ago

Was this helpful?

arduino-bluetooth-car/bluetoothCarCode.ino at main · RoboFactorySA/arduino-bluetooth-carGitHub
Logo