Arduino Part 2
Arduino Part 2
To control the direction of the spin of DC motor, without changing the way that the leads
are connected, you can use a circuit called an H-Bridge. An H bridge is an electronic cir-
cuit that can drive the motor in both directions. H-bridges are used in many different
applications, one of the most common being to control motors in robots. It is called an
H-bridge because it uses four transistors connected in such a way that the schematic dia-
gram looks like an "H."
The L298N Motor Driver is a controller that uses an H-Bridge to easily control the direc-
tion and speed of up to 2 DC motors. This tutorial will show you how to control a motor
using L298N. A simple program on Arduino platform to control 3 pins is able to control
the speed and the spin (forward or backward) of the motor.
Secondly, the Serial Monitor is an essential tool when creating projects with Arduino. It
can be used as a debugging tool, testing out concepts or to communicate directly with the
Arduino board.
The TinkerCad environment has the Serial Monitor tool integrated with the editor, which
means that an Arduino board can send and receive data from the Serial Monitor. This
interface is useful to extend the connection to other devices such as a Bluetooth device
(e.g. HC06) for controlling remotely the system.
On most Arduino boards, it is important to notice that the PWM function is available on
pins 3, 5, 6, 9, 10, and 11. These pins have both digitalWrite and analogWrite function.
Other output pins only have digitalWrite function. The first demonstration to compare
analog and digital function is proposed in the circuit bellow, with 2 different LEDs, con-
nected to pin number 2 and 3.
https://www.tinkercad.com/things/dNofqdRzDh1
The brightness of the LED connected to pin number 3 is increased from the minimum
to the maximum, which can be compared to the brightness of the LED connected to pin
number 2. This LED is set to the maximum brightness by using the digitalWrite function.
The source code of this demo is shown bellow:
1 void setup () {
2 pinMode (2 , OUTPUT ) ;
3 pinMode (3 , OUTPUT ) ;
4 digitalWrite (2 , HIGH ) ;
5 }
6
7 void loop () {
8 for ( int i = 0; i < 255; i += 5) {
9 analogWrite (3 , i ) ;
10 delay (100) ;
3 Motor Controller
From this section, students are proposed to construct their programs to control 2 motors,
which are normally used for a robot movement. The schematic of the simulation is pro-
posed as following.
https://www.tinkercad.com/things/5fQLcEA1P4Q
1 void setup () {
2 pinMode (8 , OUTPUT ) ;
3 pinMode (11 , OUTPUT ) ;
4 pinMode (9 , OUTPUT ) ;
5 }
6 void loop () {
7 digitalWrite (11 , HIGH ) ;
8 digitalWrite (8 , LOW ) ;
9 analogWrite (9 , 100) ;
10 }
5 } else {
6 speed = 0 - speed ;
7 }
8 }
2 void loop () {
3 // TODO : Implement testing script
4 }
2 void loop () {
3 // TODO : Implement testing script
4 }
What does happen if the numer 300 is used in analogWrite function? Briefly provide
your answer in the report.
4 Serial Monitor
In data transmission, serial communication is the process of sending data one bit at a
time, sequentially, over a communication channel or computer bus. This is in contrast
to parallel communication, where several bits are sent as a whole, on a link with several
parallel channels. The serial communication is available in most of micro-processor and
micro-controller systems, such as the PC, Smart Phone or the Arduino board.
In this lab, a serial communication is used for data transmission between the Arduino
board and a computer or other devices. All Arduino boards have at least one serial port
(also known as a UART or USART) named Serial. It communicates on digital pins 0 (RX)
and 1 (TX) as well as with the computer via USB. Thus, if you use these functions, you can-
not also use pins 0 and 1 for digital input or output. To activate the serial communication,
following source code is required in the setup function.
1 void setup () {
2 Serial . begin (115200) ;
3 Serial . println ( " Hello TinkerCad " ) ;
4 }
From the TinkerCad terminal, you can send some data to the Arduino board by pressing
any charater on your PC keyboard and the click buttion Send. However, some code need
to be implemented in the Arduino board as following: the board keep checking if there is
a character sent to it. If there is a character, it will read this character and send it back to
the PC terminal.