Building Arduino Cars - The Complete Reference
Building Arduino Cars - The Complete Reference
This book contains everything you need know to build your own Arduino-
based electronic car. Enjoy building Arduino cars!
2016 - 2021.
Table of Contents
Chapter 1: Components
The first step is to get to know the components of an electronic car. This
chapter is about various components used in electronics cars.
Chapter 2: Wiring
Chapter 3: Coding
Helps you to complete the most challenging step of writing your car control
code. As you build your first car, codes can be downloaded. But this
chapter explains everything that's in the downloaded file.
Chapter 4: Uploading
Chapter 5: Controlling
The most fun part of the book. This chapter discusses installing the app and
controlling your first Arduino car with a phone.
Chapter 1: Components
Bill of Materials: BoM is a list of all the required raw
materials, components, parts, and quantity of each ingredient
to building an end product. Read more about BoMs.
Troniction has listed all the required raw materials, components, parts, and
tools to build your first electronic car. This will make your life easy by not
letting you forget any component when you are in the stores to buy
components.
It delays your project when you found you don't have a required component
in the middle of your project. If you go through Troniction BoM and take
note of all the things, you will have more time to learn and build your
electronics car.
Links: Troniction website has linked to sites where you can purchase the
items yourself.
Buzzer
Two Batteries
Battery Holder
Battery Charger
Chassis with 4 Wheels Pack
Circuit Wire
Small Bolts
Tools Needed
Pointed pliers
Philips screwdriver
Soldering Wires
Soldering Iron
Other Requirements
See the below diagram, the same side wires are not parallel. The wires are
interchanged. See the front two motor wirings, the wires are interchanged to
get the same direction.
Warning: If you connect the wires parallelly and symmetrically then the
wheels will rotate in opposite directions. You may have to change the
wiring later.
Always connect the wires as shown in the diagram below. (Change the
polarities of the wires when the gear motor rotates 90 degrees).
Connect Motor Driver L298N to Arduino
Connecting the motor driver L298N to Arduino is pretty straightforward. Just
use a size 6 ribbon cable and connect the two as shown in the below table.
arduinocar.ino
Core Car Code
This is the core software that enables Troniction car running with Bluetooth
control.
pitches.h
Musical Note Definitions
This file defines the notes required to play the 'Happy Birthday' song by
Troniction car.
# include "pitches.h"
# define enB 8
# define in4 9
# define in3 10
# define in2 11
# define in1 12
# define enA 13
# define buzzerpin 2
char btsignal;
void setup () {
pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT);
pinMode(buzzerpin, OUTPUT);
set_speed( 150 );
Serial.begin( 9600 );
void loop () {
else {
reversing = false ;
digitalWrite(in1, HIGH); digitalWrite(in2, HIGH); digitalWrite(in3, HIGH); digitalWrite(in4, HIGH);
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED previousMillis
= currentMillis;
// Reversing tone
if (reversing == true ) {
tone(buzzerpin, 5000 ,interval);
}
}
}
int melodyHappy[] = {NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4, NOTE_C4, NOTE_C4,
NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4,
NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_AS4, NOTE_AS4,
NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4};
int noteDuration = 0 ;
int pauseBetweenNotes = 0 ;
int sizeMelody = sizeof (melodyHappy)/ sizeof ( int );
delay(pauseBetweenNotes);
}
}
if (btsignal== 'F' ){
digitalWrite(in1, LOW); digitalWrite(in2, HIGH); digitalWrite(in3, HIGH); digitalWrite(in4, LOW);
File 2: pitches.h
# define NOTE_B0 31
# define NOTE_C1 33
# define NOTE_CS1 35
# define NOTE_D1 37
# define NOTE_DS1 39
# define NOTE_E1 41
# define NOTE_F1 44
# define NOTE_FS1 46
# define NOTE_G1 49
# define NOTE_GS1 52
# define NOTE_A1 55
# define NOTE_AS1 58
# define NOTE_B1 62
# define NOTE_C2 65
# define NOTE_CS2 69
# define NOTE_D2 73
# define NOTE_DS2 78
# define NOTE_E2 82
# define NOTE_F2 87
# define NOTE_FS2 93
# define NOTE_G2 98
# define NOTE_GS2 104 # define NOTE_A2 110 # define NOTE_AS2 117 # define NOTE_B2 123 #
define NOTE_C3 131 # define NOTE_CS3 139 # define NOTE_D3 147 # define NOTE_DS3 156 #
define NOTE_E3 165 # define NOTE_F3 175 # define NOTE_FS3 185 # define NOTE_G3 196 #
define NOTE_GS3 208 # define NOTE_A3 220 # define NOTE_AS3 233 # define NOTE_B3 247 #
define NOTE_C4 262 # define NOTE_CS4 277 # define NOTE_D4 294 # define NOTE_DS4 311 #
define NOTE_E4 330 # define NOTE_F4 349 # define NOTE_FS4 370 # define NOTE_G4 392 #
define NOTE_GS4 415 # define NOTE_A4 440 # define NOTE_AS4 466 # define NOTE_B4 494 #
define NOTE_C5 523 # define NOTE_CS5 554 # define NOTE_D5 587 # define NOTE_DS5 622 #
define NOTE_E5 659 # define NOTE_F5 698 # define NOTE_FS5 740 # define NOTE_G5 784 #
define NOTE_GS5 831 # define NOTE_A5 880 # define NOTE_AS5 932 # define NOTE_B5 988 #
define NOTE_C6 1047 # define NOTE_CS6 1109 # define NOTE_D6 1175 # define NOTE_DS6
1245 # define NOTE_E6 1319 # define NOTE_F6 1397 # define NOTE_FS6 1480 # define
NOTE_G6 1568 # define NOTE_GS6 1661 # define NOTE_A6 1760 # define NOTE_AS6 1865 #
define NOTE_B6 1976 # define NOTE_C7 2093 # define NOTE_CS7 2217 # define NOTE_D7 2349
# define NOTE_DS7 2489 # define NOTE_E7 2637 # define NOTE_F7 2794 # define NOTE_FS7
2960 # define NOTE_G7 3136 # define NOTE_GS7 3322 # define NOTE_A7 3520 # define
NOTE_AS7 3729 # define NOTE_B7 3951 # define NOTE_C8 4186 # define NOTE_CS8 4435 #
define NOTE_D8 4699 # define NOTE_DS8 4978
Let’s discuss the code in detail. Let’s take the main Troniction car code file.
The explanation of code can be divided into 5 main sections.
You can add programming instructions in one file to another using ‘File
Includes’. You can use the ‘#include’ directive to tell the program to
include the file after the name.
#include "pitches.h"
Why this is important? You can write commonly used code in one file and
use it again and again by including it in other files. That way you don’t
have to repeat writing the same code again and again.
In this program, we have separated the musical note definitions to another file
called ‘pitches.h’ and included it in our main Troniction car code.
#define enB 8
#define buzzerpin 2
Once you define Arduino pin number 2 as ‘buzzerpin’, it is easier to write the
program. It is easier to understand what you have written at a later date.
Some values in our car control program vary over time. For example, the
signal sent by phone via Bluetooth continuously varies when we press
different buttons. Therefore we need some identifiers to remember the latest
value.
char btsignal;
We can define the character variable ‘btsignal’ to capture the signal sent from
the phone via Bluetooth.
Pin modes are defined here. You can define an Arduino pin to be an INPUT
or an OUTPUT. here we have defined the enA pin as an OUTPUT pin.
pinMode(enA, OUTPUT);
Arduino boards work in digital. Digital output pins can either be a high
voltage (+5V) or a low voltage (0). You can programmatically define
what is the output voltage of a pin should be. The following line of code
sets one pin (in1) of the Arduino board to HIGH voltage (+5V).
digitalWrite(in1, HIGH);
You can write functions in Arduino. If some code lines are going to repeat
over and over, then you can create a function call the function again and
again without writing the same code again and again.
analogWrite(enA, speed);
analogWrite(enB, speed);
}
When we receive signals from the phone via Bluetooth we have to change the
speed of the
motors again and again depending on the speed signal. So we have created a
separate
function to handle the code repeating issue. Now we only have to call the
function with
speed value so that the writing speed to the PWM pins happens
automatically.
set_speed(100)
Void: Function’s return type ‘void’ means the function doesn’t output or
return anything, it simply set output pin status.
There is another function in our file. That is for playing the Happy Birthday
song.
void play_happy_birthday(){ ... }
The code instructions inside the ‘loop function’ will be executed again and
again, hence the word loop.
The Troniction software will read the Bluetooth signal from your phone again
and again. Then it will decide what to do according to the received signal.
Table of possible signals from your phone and actions how to interpret and
actuate them as car actions.
The last piece of code generates a beep tone when you reverse the Troniction
car.
You can write code in the editor and save them. But you can’t upload the
code to the board yet. You need to install the Arduino plugin. Let’s install
the Arduino plugin now.
Here you can download the plugin, if it is not already installed on your
system.
Click DOWNLOAD to start the download.
The zip file contains the code to control Arduino car via Bluetooth.
Download Zip Archive
Go to Sketchbook > Import Icon. Upload the zip file you just downloaded.
You can see the code in your editor window.
First you need to select the bluetooth device of your car from the settings. It
will be listed as HC-06.
Search for Arduino Bluetooth RC Car to download the app.
In Option Menu > Settings, you can find all the characters that are being sent
when you press each of the buttons in the app interface.