0% found this document useful (0 votes)
138 views

Building Arduino Cars - The Complete Reference

This document provides instructions for building an Arduino-based electronic car using the Troniction kit. It includes 5 chapters that cover the components, wiring, coding, uploading code, and controlling the car with a smartphone. The coding chapter explains how to download pre-written code files, displays their contents, and provides an in-depth explanation of how the code works.

Uploaded by

djedaiy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views

Building Arduino Cars - The Complete Reference

This document provides instructions for building an Arduino-based electronic car using the Troniction kit. It includes 5 chapters that cover the components, wiring, coding, uploading code, and controlling the car with a smartphone. The coding chapter explains how to download pre-written code files, displays their contents, and provides an in-depth explanation of how the code works.

Uploaded by

djedaiy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Introduction

This book contains everything you need know to build your own Arduino-
based electronic car. Enjoy building Arduino cars!

Troniction by Range Intec


https://troniction.com
https://www.facebook.com/troniction
https://www.instagram.com/troniction
https://twitter.com/troniction
https://www.facebook.com/groups/troniction

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

Step by step illustrated instructions on how to connect your components to


make your first Arduino-based car.

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

Clear steps and instructions on uploading your code to the car.

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.

Arduino Uno Board with USB Cable

Bluetooth Module HC6


Motor Driver L298N

Buzzer
Two Batteries

Battery Holder
Battery Charger
Chassis with 4 Wheels Pack

Male to female ribbon cable

Circuit Wire
Small Bolts
Tools Needed

Pointed pliers

Philips screwdriver
Soldering Wires

Soldering Iron
Other Requirements

A laptop/computer to program the board


Transparent Plastic Enclosure Box

A smart mobile phone to control the car


Chapter 2: Wiring
Step By Step Walkthrough - Wiring the Troniction
Arduino Car

1. Connect Motor Driver L298N to Gear Motors L298N to Gear Motors


2. Connect Motor Driver L298N to Arduino L298N to Arduino
3. Connect Bluetooth Module HC-06 to Arduino HC-06 to Arduino
4. Connect Buzzer to Arduino
5. Connect Battery Pack to Arduino
Click here to download a high-resolution image

Connect Motor Driver L298N to Gear Motors of the


Chasis
Tip: When the gear motor position changes direction perpendicularly, wires
should also be interchanged to get the same rotating direction.

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.

Arduino Pin L298N Pin


8 EnB
9 IN4
10 IN3
11 IN2
12 IN1 13 EnA

Connect Bluetooth Module HC-06 to Arduino


Connect Buzzer to Arduino

Connect Battery Pack to Arduino


Chapter 3: Coding
Arduino Car Coding
You have to write software to control the car via Bluetooth.
The Troniction car coding page is divided into three sections.

Section 1. Download the completed source code files


which are ready to upload.

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.

Section 2. Display of file contents in each source


file.
File 1: arduinocar.ino

# 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;

bool reversing = false ;

unsigned long previousMillis = 0 ; const long interval = 100 ;

void setup () {
pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT);

pinMode(enB, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT);

pinMode(buzzerpin, OUTPUT);
set_speed( 150 );

digitalWrite(in1, HIGH); digitalWrite(in2, HIGH); digitalWrite(in3, HIGH); digitalWrite(in4,


HIGH);

Serial.begin( 9600 );

void loop () {

if (Serial.available()){ btsignal= Serial.read(); //Serial.println(t); }

else if (btsignal== 'R' ){ digitalWrite(in1, LOW); digitalWrite(in2, HIGH); digitalWrite(in3, LOW);


digitalWrite(in4, HIGH);
}

else if (btsignal == '0' ){ set_speed( 150 );


}

else if (btsignal == '1' ){ set_speed( 150 );


}

else if (btsignal == '2' ){ set_speed( 170 );


}

else if (btsignal == '3' ){ set_speed( 180 );


}

else if (btsignal == '4' ){ set_speed( 200 );


}

else if (btsignal == '5' ){ set_speed( 210 );


}

else if (btsignal == '6' ){ set_speed( 220 );


}

else if (btsignal == '7' ){ set_speed( 230 );


}

else if (btsignal == '8' ){ set_speed( 240 );


}

else if (btsignal == '9' ){ set_speed( 250 );


}

else if (btsignal == 'W' ){ //frontlight = true;


}

else if (btsignal == 'w' ){ //frontlight = false;


}

else if (btsignal == 'V' ){ tone(buzzerpin, 2000 , 1000 );


}

else if (btsignal == 'v' ){ tone(buzzerpin, 2000 , 1000 );


}

else if (btsignal == 'U' ){ //backlight = true;


}

else if (btsignal == 'U' ){ //backlight = false;


}

else if (btsignal == 'X' ){ play_happy_birthday();


}

else if (btsignal == 'x' ){


play_happy_birthday();
}

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);
}
}
}

void set_speed ( int speed){


analogWrite(enA, speed);
analogWrite(enB, speed);
}

void play_happy_birthday (){

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 timeHappy[] = {4, 4, 2, 2, 2, 1, /*line 1*/


4, 4, 2, 2, 2, 1, /*line 2*/
4, 4, 2, 2, 4, 4, 2, 1, /*line 3*/
4, 4, 2, 2, 2, 1};

int noteDuration = 0 ;
int pauseBetweenNotes = 0 ;
int sizeMelody = sizeof (melodyHappy)/ sizeof ( int );

for ( int i = 0 ; i < sizeMelody; i++) {

noteDuration = 1000 / timeHappy[i];

tone(buzzerpin, melodyHappy[i], noteDuration);

pauseBetweenNotes = noteDuration * 1.30 ;

delay(pauseBetweenNotes);

}
}

if (btsignal== 'F' ){
digitalWrite(in1, LOW); digitalWrite(in2, HIGH); digitalWrite(in3, HIGH); digitalWrite(in4, LOW);

else if (btsignal== 'B' ){ reversing = true ;


digitalWrite(in1, HIGH); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, HIGH);
}

else if (btsignal== 'L' ){ digitalWrite(in1, HIGH); digitalWrite(in2, LOW); digitalWrite(in3, HIGH);


digitalWrite(in4, LOW);
}
else if (btsignal == 'q' ){
set_speed( 255 );
}

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

Section 3. An in-depth explanation of the codes


in the source files.

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.

Section 3.1: File includes

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.

Advantage of having musical notes in a separate file called ‘pitches.h’: On a


later day, you can use the same file to define a song other than ‘Happy
Birthday’ in one of your programs for Arduino.

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.

Note: .h extension is used to identify header files.

Section 3.2: Pin Definitions

Using numbers as Arduino pin identifiers is very difficult to handle. Instead,


you can define names for pin numbers.

#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.

‘#define’directive is used to define names for Arduino pin numbers.

Section 3.3: Variable Definitions

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.

const long interval = 100;

Here we have defined the interval variable as 100 milliseconds. When we


reverse the Troniction car the time interval between beep sounds is 100
milliseconds.

Section 3.4: Set up Function

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);

Pin mode: Whether the pin is an input or an 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);

Output pin status: Whether the pin is high or low.

Set Speed Function:

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.

void set_speed(int speed){

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)

Function: Enables us to create re-usable code snippets.

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 advantage of having the song code in a separate function is whenever we


need to play the song, we can just call ‘play_happy_birthday’. (E.g. when a
button is pressed we can call this function and play the happy birthday
song.)
Section 3.5: Loop Function

The code instructions inside the ‘loop function’ will be executed again and
again, hence the word loop.

void 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.

Bluetooth Signal Car Action


F Go Forward
B Reverse
L Turn Left
R Turn Right
1 Speed 150
9 Speed 250
V Horn
X Play Happy Birthday

The last piece of code generates a beep tone when you reverse the Troniction
car.

unsigned long currentMillis = millis();


if (currentMillis - previousMillis >= interval) { previousMillis =
currentMillis;
if (reversing == true) {
tone(buzzerpin,5000,interval);
}
}
Chapter 4: Uploading
You can access Arduino Create web editor by vising
https://create.arduino.cc/editor
Visit Arduino Create Editor

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.

Arduino Web Editor Plugin Installation


1. Visit Plugin Welcome Page

Visit Plugin Welcome Page


Click START.

2. Download the Arduino Plugin

Here you can download the plugin, if it is not already installed on your
system.
Click DOWNLOAD to start the download.

3. Install the Arduino Plugin


After downloading, extract the compressed file and double click to start the
installation wizard.
4. Plugin Installation Successful
After successful installation of the Arduino plugin, you get the following
screen. Congratulations! You are all set.
Now click GO TO WEB EDITOR to start uploading the code to the board.

Uploading the Arduino Car Sketch


Download the Zip File

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.

Click on Upload Icon to Upload the Sketch to the Board.


Chapter 5: Controlling
You can send control signals to your Arduino car via the following app.

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.

You might also like