🤖
KidsCode
  • Welcome to KidsCode
  • Setting up mBlock
  • Arduino Lessons
    • Arduino Beginner Lessons
      • Blinking an LED
      • LED Switching
      • LED Chasing
      • Traffic Signal
      • Buzzer
      • Buzzer + Push Button
      • LED + Push Button
    • Bluetooth LED
  • Arduino Robot Car
    • Arduino Bluetooth Car
      • What you will need
      • Circuit Diagram
      • Assembly the Car
      • Upload the Code
      • Bluetooth setup and Test
  • Micro:bit Beginners
    • micro:bit, the hardware
    • Overview of MakeCode
    • Video Library
    • Lesson 1
      • My micro:bit
      • Safety first!
      • Plug in your micro:bit
      • My name is?
      • Learning Outcomes
    • Lesson 2
      • Whats your name again?
      • How fast is your heart beat?
    • Lesson 3
      • Please switch on the TV
      • One, Two, Three
      • Keep pressing that buttons
    • Lesson 4
      • Variable, what is that!!
      • Step Counter
      • 1 + 1 = 2
  • Micro:bit Advanced Activities
    • Blinking an LED
    • Traffic Signal
Powered by GitBook
On this page
  • Code upload
  • Install the Arduino App

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;

    }
  }
}

Once the code is uploaded successfully, connect RX of Hc-05 to TX of Arduino and TX of Hc-05 to RX of Arduino

Install the Arduino App

PreviousAssembly the CarNextBluetooth setup and Test

Last updated 4 years ago

Was this helpful?

Install the App.

Arduino Bluetooth RC Car