arduino robotic car - esjstemws.org · arduino button near the top of the arduino menu, turn off...

14
Version 7, 11/5/18 with Bluetooth, Smartphone and Obstacle Addendums Allan Thompson Arduino Robotic Car Introduction The Emgreat® Motor Robot Car Chassis Kit and the DROK L298N Motor Drive Controller Board appear to be a good way for STEM Workshop students to construct an Arduino controlled, model robotic car. The robotic car chassis kit greatly simplifies the construction and facilitates creative car designs and board mounting options. Students can use Snap4Arduino to experiment with controlling the motors (e.g. using the arrow keys on the keyboard to move the car forward and backward, and turn right and left, and using other keys to adjust the speed of each motor). Other components (e.g., Bluetooth, LEDs and sensors) can be added for wireless communications, to enhance the design and/or to detect obstacles in the car’s path. A Bluetooth wireless connection (Addendum 1) allows the robotic car to be controlled without a USB cable connected. The car can also be controlled by a smartphone app (Addendum 2). An ultrasonic obstacle detector can be added (Addendum 3) so the robotic car can detect obstacles, measure the distance and take appropriate actions if needed to avoid collisions. This is the first step in building an autonomous robotic car that can make decisions by itself and take appropriate actions. The result is a low cost and effective project for the Union Pacific Railroad grant. Table of Contents Emgreat® Motor Robot Car Chassis Kit ......................................................................................................... 2 DROK L298N Motor Drive Controller Board ................................................................................................. 2 Mounting Considerations ............................................................................................................................. 3 Connections .................................................................................................................................................. 4 Basic Testing.................................................................................................................................................. 6 Snap4Arduino ............................................................................................................................................... 6 Prototype .................................................................................................................................................... 10 Conclusion ................................................................................................................................................... 12 References ................................................................................................................................................. 12 Attachment 1 – Arduino Motor Controller Test Program .......................................................................... 13 Addendum 1 – Bluetooth Addendum 2 – Smartphone Operation Addendum 3 – Obstacle Detection Versions Version 4 added Addendum 2 for Smartphone Operation. Version 5 includes modifications to Addendum 2 to change how the car turns when controlled by a smartphone and a section on reliability. Version 6 includes Addendum 3 on obstacle detection so the robotic car can detect obstacles, measure the distance and take appropriate actions if needed to avoid collisions. Version 7 adds fritzing connection illustrations to the description below and to Addendums 1 and 3.

Upload: truongkhuong

Post on 13-Nov-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Version 7, 11/5/18 with Bluetooth, Smartphone and Obstacle Addendums Allan Thompson

Arduino Robotic Car

Introduction The Emgreat® Motor Robot Car Chassis Kit and the DROK L298N Motor Drive Controller Board appear to be a good way for STEM Workshop students to construct an Arduino controlled, model robotic car. The robotic car chassis kit greatly simplifies the construction and facilitates creative car designs and board mounting options. Students can use Snap4Arduino to experiment with controlling the motors (e.g. using the arrow keys on the keyboard to move the car forward and backward, and turn right and left, and using other keys to adjust the speed of each motor). Other components (e.g., Bluetooth, LEDs and sensors) can be added for wireless communications, to enhance the design and/or to detect obstacles in the car’s path. A Bluetooth wireless connection (Addendum 1) allows the robotic car to be controlled without a USB cable connected. The car can also be controlled by a smartphone app (Addendum 2). An ultrasonic obstacle detector can be added (Addendum 3) so the robotic car can detect obstacles, measure the distance and take appropriate actions if needed to avoid collisions. This is the first step in building an autonomous robotic car that can make decisions by itself and take appropriate actions. The result is a low cost and effective project for the Union Pacific Railroad grant.

Table of Contents Emgreat® Motor Robot Car Chassis Kit ......................................................................................................... 2 DROK L298N Motor Drive Controller Board ................................................................................................. 2 Mounting Considerations ............................................................................................................................. 3 Connections .................................................................................................................................................. 4 Basic Testing .................................................................................................................................................. 6 Snap4Arduino ............................................................................................................................................... 6 Prototype .................................................................................................................................................... 10 Conclusion ................................................................................................................................................... 12 References ................................................................................................................................................. 12

Attachment 1 – Arduino Motor Controller Test Program .......................................................................... 13

Addendum 1 – Bluetooth

Addendum 2 – Smartphone Operation

Addendum 3 – Obstacle Detection

Versions Version 4 added Addendum 2 for Smartphone Operation.

Version 5 includes modifications to Addendum 2 to change how the car turns when controlled by a smartphone and a section on reliability.

Version 6 includes Addendum 3 on obstacle detection so the robotic car can detect obstacles, measure the distance and take appropriate actions if needed to avoid collisions.

Version 7 adds fritzing connection illustrations to the description below and to Addendums 1 and 3.

Page 2: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Arduino Robotic Car

2

Emgreat® Motor Robot Car Chassis Kit The Emgreat® Motor Robot Car Chassis Kit (Reference 1) includes the 3V to 12V DC motors, wheels, plastic base, 6V battery holder and mounting hardware. This kit greatly simplifies the basic construction and the pre-drilled holes facilitate creative mounting options for the Arduino and motor controller boards and the battery holder and other components.

DROK L298N Motor Drive Controller Board The DROK L298N Motor Drive Controller Board (References 2-4) is a Dual Motor Controller Module. It includes an H-bridge to control the speed and direction of two DC motors, or control one bipolar stepper motor. Notes:

1. The +12V terminal is the connection for the + side of the power supply for the motors. This module can be used with motors that have a voltage of between 5 and 35V DC (e.g., the red lead of the 6V battery pack supplied with the chassis kit). Remove the 12V jumper only if the supply voltage is greater than 12V which enables power to the onboard 5V regulator.

2. The motor direction is controlled by sending a HIGH or LOW signal to the drive for each motor (or channel). For example, for Motor 1, a HIGH to IN1 and a LOW to IN2 will cause it to turn in one direction, and a LOW and HIGH will cause it to turn in the other direction. Setting them both to LOW will stop the motor. However, the motors will not start until a HIGH is set to the enable pin (ENA for Motor 1). And they can be turned off with a LOW to the same pin(s). The jumpers on the enable pins must be removed for DC motor operation. To control the speed of the motors, use a PWM (pulse width modulation) signal from the appropriate Arduino digital pins connected to the motor controller enable pins.

3. The GND terminal is the ground connection for the battery pack for the motors. It must also be connected to GND on the Arduino board.

4. The 5V Out terminal is an output if the 12V jumper is in place and can be used for powering for the Arduino board or other purposes. No connection is needed when a USB cable or separate battery pack is used for the Arduino board.

Page 3: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Arduino Robotic Car

3

Mounting Considerations The motors and the caster should be mounted as shown in the instructions that come with the chassis kit and the first illustration above. However, it’s important to think about the mounting options for the other components before drilling additional holes in the plastic base.

1. The first question is do you want the car to push or pull the caster (i.e., is the wide end of the plastic base at the front or the back)? Either is acceptable and I chose to pull the caster when the car moves forward with the wide end of the base at the front to protect the wheels from obstacles.

2. The next question is where to mount the Arduino and motor controller boards on the base. The motor controller board will fit conveniently between the motor wheels. The Arduino board should be at the rear of the unit with the USB cable at the back end so the car doesn’t try to drive over the cable when moving forward (the cable can be held up when the unit is moving backward). The USB connector should be at the center of the back end so the cable drag doesn’t turn the car.

3. Another important question is how to mount the Arduino and motor controller boards on the base. I chose to use wood strips initially for this project so the boards could be mounted easily without drilling additional holes in the plastic base. Then the board placement can be adjusted and optimized later after testing the operation. This may also be the best plan for the students since it is much easier to mount the boards with screws on wood strips than to drill additional holes and use standoffs and small bolts. Standoffs are needed for the motor controller board in any case because the solder connections protrude from the bottom of the board near the mounting holes.

4. Weight distribution is another consideration. The battery pack is usually the heaviest component. Do you want it over the motor wheels to provide optimum traction? I found that it should not be at the front because then the car will lift the caster when it starts forward at high power. I used a 9V battery rather than the battery pack supplied with the chassis kit. The battery pack could also be mounted on the bottom of the plastic base to optimize the placement and provide additional room for other components on the top of the base. A tie wrap or Velcro can be used to hold the batteries in the battery holder. In any case, a switch should be used in the plus side of the battery pack (red wire) so it is easy to turn off power to the motors. Since a 9V battery is smaller than the battery pack, I had room to mount it and the switch at the rear end near the Arduino board.

5. Another question is do you want to include a breadboard (e.g., the breadboards that come with Arduino kits) on your unit to mount and test additional components like LEDs and sensors? I put one on the front of my test unit for LEDs to monitor the motor signals and to mount other components (see below).

My test configuration is shown is the photo below (Page 11). As you work your way through this document, it’s important to assemble the car in small increments and test it before going on to the next step. For example, I tested the motors and the motor controller before I mounted them on the chassis just to be sure I knew how to make the connections and that I wasn’t wasting my time assembling parts that wouldn’t work. Be sure to use the motor controller test program and then the Snap4Arduino scripts before you try to add Bluetooth or use the smartphone app. The car will probably never work if you try to assemble everything without testing it step by step. And you will learn so much during the process that may change your final design for the car. I tried to encourage incremental assembly and testing by taking a step-by-step approach in this document but I want to emphasize that incremental assembly and testing is the key to success for any project.

Page 4: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Arduino Robotic Car

4

Connections The connections for the Arduino pins are in the table below. A header or wires with female plugs should be used for the motor controller connections to avoid solder on pins that jumpers may be installed on later.

Arduino Pins Motor Controller Pins Purpose

Motor 1

10 ENA (Remove Jumper) Motor 1 Enable (PWM 0 to 255)

9 IN1 Motor 1 Direction (High or Low)*

8 IN2 Motor 1 Direction (High or Low)*

Motor 2

5 ENB (Remove Jumper) Motor 2 Enable (PWM 0 to 255)

7 IN3 Motor 2 Direction (High or Low)*

6 IN4 Motor 2 Direction (High or Low)*

GND GND Ground for Arduino connections

* See Note 2 above. The breadboard view for these connections is shown in the figure below. A breadboard is used for these connections so LEDs and other components can be added later. The Arduino and Motor Controller connections are the same as the table above because the five holes in each horizontal row of the breadboard are connected together. The Motor Controller board is rotated 180 degrees in this figure from the photos below to facilitate illustrating the connections. The Arduino pins can be connected directly to the Motor Controller for testing or debugging but it is useful to make the connections to the breadboard as shown now so they can be tested with the motors.

The connections are shown as individual wires but it is helpful to use male-to-male Dupont Wire cables with multicolored wires fastened together for each of the four sets of three wires to facilitate debugging and improve reliability. These cables can be separated at the ends to go to various locations on the breadboard. It’s not important to use the same colors as this figure but don’t use red and black because they are used for power and ground. The Motor Controller board is red in the illustration above but the actual color is dark green as shown in the photos below.

Page 5: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Arduino Robotic Car

5

The other Motor Controller connections are:

Connection Controller Terminals Notes

Motor 1 + Out1 (Left Blue Connector Back) Reverse motor connections if motors turn in wrong direction Motor 1 - Out2 (Left Blue Connector Front)

Motor 2 + Out3 (Right Blue Connector Front) Reverse motor connections if motors turn in wrong direction Motor 2 - Out4 (Right Blue Connector Back)

Motor Power + +12V (Max 35V, Remove Jumper if > 12V) I’m using a 9V battery*

Motor Power - GND (Connect to Battery - and Arduino GND) Note two connections

Not used +5V Out if 12 Jumper (e.g., for Arduino) Not used for USB to Arduino

* The voltage to the motors should not exceed the 12V maximum specified for these motors (the documentation cautions that higher voltages will burn out the motors).

These connections are illustrated in the figure below. The LEDs described in Other Components below are also shown here because they are helpful for debugging. The motor connections are shown across the Motor Controller Board because it is rotated 180 degrees in this illustration. The motor connections can be under the base if you use this orientation and come through the holes in the base near their connection blocks on the Motor Controller Board.

Page 6: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Arduino Robotic Car

6

The switch and 9V battery provide a separate power source for the motors. This is useful for debugging and prevents voltage variations from affecting the Arduino board when the motors operate. The LEDs are powered by the connections from the Arduino board and no power connection to the breadboard is needed at this point. The motor connections use a lot of Arduino pins so the usage of other Arduino pins must be planned carefully. For example, the encoder wheels for Photoelectric Encoders (Reference 5) are included in the chassis kit but the encoders take Arduino pins and are not needed for most applications. The references below have a lot of good information (not all easy to understand or correct).

Basic Testing When the connections have been made and checked, the basic operation can be tested and observed using the Arduino test program from Reference 3 which is reproduced in Attachment 1. This demo program first turns the motors on and runs them at a PWM value of 200. This is not a speed value, rather power is applied for 200/255 of an amount of time and then repeated. Then, after a moment, the motors operate in the reverse direction (because the HIGHs and LOWs are changed in the digitalWrite() functions). To get an idea of the range of speed possible of your unit, the program then runs through the entire PWM range which turns the motors on and them runs through PWM values zero to 255 and back to zero using the two for loops. If the motors do not operate properly, check and adjust the connections and hardware until the correct operation is observed. If the wheels don’t turn in the same direction, check the Arduino connections to the motor controller and, if they are correct, reverse the leads to the motor that is turning in the wrong direction. Look at the code and comments in Attachment 1 to understand the Arduino operations needed to control the motors. demoOne and demoTwo are functions (subprograms) which are called by the four-line main program in the loop at the very end of the program. This test program is an effective way to test that all the Arduino and motor controller connections are correct and that the motors are working properly. However, there is not a convenient way to write and experiment with a simple Arduino program to control a robotic car where we want some keys (e.g., the keyboard arrow keys) to control the direction the robotic car moves and turns.

Snap4Arduino Snap4Arduino is an effective and convenient platform for creating and testing simple programs for this robotic car. Snap4Arduino (http://snap4arduino.rocks) is a modification of the Snap! visual programming language that seamlessly interacts with almost all versions of the Arduino board. You can either use the online version of Snap4Arduino or download the appropriate version for your computer. In either case, be sure to set up a SNAP account so you can save, retrieve and share your programs. StandardFirmata must be loaded on the Arduino board (using the Arduino IDE). Look under File, Examples, Firmata for StandardFirmata (and install the library if needed). One important advantage of

Page 7: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Arduino Robotic Car

7

using Snap4Arduino for testing is that the all changes are made in the Snap4Arduino programs which is much easier and faster than making changes in Arduino code and reloading it.

A. Basic Motor Functions The first step is to create functions (blocks) shown in the figure below to control the basic operations for each motor (forward, backward and stop). Making these blocks first will simplify the programs (scripts) for the keys that control the motors because these blocks contain the commands needed for each motor operation so they don’t have to be repeated many times in the main program.

Page 8: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Arduino Robotic Car

8

In the Snap4Arduino screen above, these new blocks are the six blocks at the bottom of the standard Arduino blocks at the left and are shown open in the editor so you can see the contents. Note that the percentage of the PWM signal (0% to 100%) is an input for the forward and backward functions and that this percentage is multiplied by 2.25 in the last block in these functions so the PWM signal varies from 0 to 255 maximum. These blocks can also be used to adjust the calibration for the motors. For example, if the car doesn’t move in a straight line when both motors are turning forward at the same percentage, reduce the 2.25 for the faster motor until the car moves in a straight line. Refer to the motor controller illustration and pin tables above to understand what each block does. For example, the Motor 1 Forward function sets Arduino pins 8 to Low and 9 to High and then sets the motor enable pin (10) to the PWM value determined by the percentage input for the function.

B. Motor Control Programs After we have created the basic motor functions, we can use these blocks in the main program (script) for the keys to operate the motors as shown in the Snap4Arduino screen below. The first step is to create two variables on the variable menu for the “speed” percentage for each motor (M1% and M2%). This percentage is used to set the PWM value in functions above but we can think of it as motor speed. The left script, which uses the keyboard arrows to control the direction of the car, first sets the variables for the speed of both motors to 50% (it can be any convenient value) and then “says” (prints) that on the stage at the right of the Snap4Arduino window (not shown above). Then it uses a forever loop to watch for keyboard arrow keys pressed and activates the appropriate motor function blocks. A series of if/else blocks is used to ensure that the program only responds to one arrow key at a time. For example, if you hold down both the up and down arrow keys, only the up arrow key is effective and the car moves forward. The bottom else block stops both motors if no arrow keys are pressed. The four scripts at the right control the two motor speed variables individually using other convenient keys. Note that these scripts should include checks so that these variables do not exceed 100% or go below 0% so the input to the motor control functions is always between 0% and 100%. If both motors are set to a high speed, and a left or right arrow key is pressed, the car will turn very sharply because the motor on the side the car is turning will rotate backwards but, if the motor on the side that the car is turning toward is set to a slower speed or zero, the car will make a wider turn. Also, if the car is moving forward, the car will turn in the direction of the slower motor unless both motor speeds are the same. This is another way to make a turn. Note that changing these variables in Snap4Arduino is communicated to the Arduino board only when the primary (left) script is running and an arrow key is pressed. The variables can be changed while the motors are stopped but nothing else happens until an arrow key is pressed. Also note that the primary script continuously detects if an arrow key is depressed. Holding the arrow key down activates continuous motion; tapping an arrow key repeatedly just sends short pulses to the motors. Variations of these scripts can be created to control the motors in other ways but the scripts should be written carefully so only one command is sent to each motor at one time and the speed is always between 0% and 100%. Otherwise, the motor operation can be confusing and/or possibly damage the motors or the motor controller board.

Page 9: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Arduino Robotic Car

9

C. Operation The steps to operate the robotic car are:

1. Connect the Arduino board to a computer and use the Arduino IDE to load StandardFirmata on the Arduino board if this has not been done previously. Once StandardFirmata has been loaded on the Arduino board, it does not need to be reloaded after the USB cable is disconnected.

Page 10: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Arduino Robotic Car

10

2. Open Snap4Arduino and your project and use the Connect Arduino button at the top of the Arduino menu to connect Snap4Arduino to the Arduino board. Retry this step and/or reset the Arduino board if needed (if connection starts but doesn’t finish, reset Arduino).

3. Turn on the power to the motor controller board using the switch on the car.

4. Click on the Snap4Arduino green flag to start the motor control script. This script must be running for the motor control keys to work.

5. Then the arrow keys and other keys can be used to operate the car. Remember that the speed control variables do not have any effect on the motors until the arrow keys are pressed.

6. When you are finished, save any changes to your Snap4Arduino scripts, click the Disconnect Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the USB cable.

D. Other Components

Other Arduino controlled components (e.g., LEDs and sensors) and scripts can be added to enhance the design to be more like a car and/or to detect obstacles in the car’s path, etc. However, the basic motor operation uses a lot of Arduino digital pins so the remaining pins must be allocated judiciously. For example, LEDs can be activated directly using the motor control pins to avoid using additional pins for lights. The negative (short) lead of each LED should be connected to ground through a 330-ohm resistor and the positive side of the LED should be connected to the appropriate motor control pins (e.g., Arduino Pins 6 or 7 for Motor 2 in the connection tables above). Connecting LEDs in this manner doesn’t change any of the connections in the connection tables; it just connects an LED and resistor in parallel with the motor control pin connection and doesn’t require any additional Arduino pins or Snap4Arduino scripts for the LEDs. Using this plan, two LEDs can be used for each motor and one will light when the motor is turning forward and the other will light when it is turning backward as one way to simulate headlights and taillights. An LED and resistor can also be connected to a motor enable pins. The motor will operate normally and the brightness of the LED varies with the motor speed (PWM value).

E. Additional Scripts Additional Snap4Arduino scripts can be used to, for example, illustrate the car operation on the stage. Any script like this for an additional sprite really operates separately from the car and does not track it directly but it is an interesting way to combine SNAP and Arduino capabilities.

Prototype A photo of my prototype is below. It looks rather messy but this is an effective platform to test the operation, add components and change the Snap4Arduino scripts as needed to test various ideas and configurations and to think about how to better place and mount the boards and components. I used yellow LEDs that light when each motor is moving forward to simulate headlights, red LEDs that light when each motor is moving backward and green LEDs for the motor enables. The wires from the motor controller pins go to the breadboard to connect them to the plus side of the LEDs and also to the wires from the Arduino pins just as if the motor controller pins were connected directly to the Arduino pins. The breadboard is just a convenient place to make this 3-way connection. There is also a ground

Page 11: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Arduino Robotic Car

11

connection from the Arduino board to the breadboard to provide ground for the six resistors for the LEDs. No 5V power connection from the Arduino board to the breadboard is needed for these LEDs because they are powered by the motor controller signals from the Arduino board when they are High. A 5V power connection from the Arduino board can be added for other components if needed. The connection tables and figures above, and the LED connection information in the Other Components section, should be used for the connections rather than looking at wires in the photo below. This photo was taken before the Bluetooth module was added to the breadboard. Connecting the Bluetooth module is described in Addendum 1.

Page 12: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Arduino Robotic Car

12

A prototype like this is too fragile for collisions but it can be used for pulling tests, shallow ramps, running mazes and similar tests. If you add obstacle detectors on each side at the front of the breadboard and the appropriate Snap4Arduino scripts, it could turn or back up to avoid obstacles. Think about what else you’d like your car to do and add scripts and components as needed. The Snap4Arduino Arduino blocks can be used to controls Arduino pins connected to LEDs and other components and to read digital and analog pins.

Conclusion The project is a low cost and effective project for the Union Pacific Railroad grant. Snap4Arduino is an effective and convenient platform for creating and testing simple programs for this robotic car. The disadvantage, of course, is that the car is connected to a computer by the USB cable but something like a keyboard is needed to control the car operation unless enough sensors are added for automatic driving to avoid obstacles. In any case, Snap4Arduino is an effective way to plan and test the desired operation. Adding a Bluetooth module to the robotic car for wireless communications and control using Snap4Arduino on a laptop or PC is described in Addendum 1. After this wireless operation using Bluetooth is successful, a smartphone can be used to control the car as described in Addendum 2. You can email me at [email protected] if you have questions or comments.

References Motors and Chassis Kit 1. https://www.amazon.com/Emgreat®-Chassis-Encoder-wheels-

Battery/dp/B00GLO5SMY/ref=sr_1_1?s=industrial&ie=UTF8&qid=1522248753&sr=8-1&keywords=emgreat+motor+robot+car+chassis+kit

Motor Controller

2. Amazon: https://www.amazon.com/DROK-Controller-H-Bridge-Mega2560-Duemilanove/dp/B00CAG6GX2/ref=sr_1_1?ie=UTF8&qid=1522243337&sr=8-1&keywords=drok+l298n+motor+drive+controller+board

3. https://tronixlabs.com.au/news/tutorial-l298n-dual-motor-controller-module-2a-and-arduino

4. http://www.instructables.com/id/Arduino-Modules-L298N-Dual-H-Bridge-Motor-Controll/ Photoelectric Encoders (if we want to use them – probably not, takes more Arduino pins) 5. HC-020K Double Speed Measuring Module with Photoelectric Encoders For Experiment https://www.amazon.com/gp/product/B00EERJDY4/ref=oh_aui_detailpage_o06_s00?ie=UTF8&psc=1 Snap4Arduino

6. http://snap4arduino.rocks

Page 13: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Arduino Robotic Car

13

Arduino Motor Controller Test Program (continued on next page) Attachment 1 This is a copy of the test program from Reference 3.

// connect motor controller pins to Arduino digital pins

// motor one

int enA = 10;

int in1 = 9;

int in2 = 8;

// motor two

int enB = 5;

int in3 = 7;

int in4 = 6;

void setup()

{

// set all the motor control pins to outputs

pinMode(enA, OUTPUT);

pinMode(enB, OUTPUT);

pinMode(in1, OUTPUT);

pinMode(in2, OUTPUT);

pinMode(in3, OUTPUT);

pinMode(in4, OUTPUT);

}

void demoOne()

{

// this function will run the motors in both directions at a fixed speed

// turn on motor A

digitalWrite(in1, HIGH);

digitalWrite(in2, LOW);

// set speed to 200 out of possible range 0~255

analogWrite(enA, 200);

// turn on motor B

digitalWrite(in3, HIGH);

digitalWrite(in4, LOW);

// set speed to 200 out of possible range 0~255

analogWrite(enB, 200);

delay(2000);

// now change motor directions

digitalWrite(in1, LOW);

digitalWrite(in2, HIGH);

digitalWrite(in3, LOW);

digitalWrite(in4, HIGH);

delay(2000);

// now turn off motors

digitalWrite(in1, LOW);

digitalWrite(in2, LOW);

digitalWrite(in3, LOW);

digitalWrite(in4, LOW);

}

void demoTwo()

{

// this function will run the motors across the range of possible speeds

// note that maximum speed is determined by the motor itself and the

operating voltage

// the PWM values sent by analogWrite() are fractions of the maximum speed

possible

// by your hardware

// turn on motors

Page 14: Arduino Robotic Car - esjstemws.org · Arduino button near the top of the Arduino menu, turn off the motor power switch on the car chassis to conserve the battery and disconnect the

Arduino Robotic Car

14

digitalWrite(in1, LOW);

digitalWrite(in2, HIGH);

digitalWrite(in3, LOW);

digitalWrite(in4, HIGH);

// accelerate from zero to maximum speed

for (int i = 0; i < 256; i++)

{

analogWrite(enA, i);

analogWrite(enB, i);

delay(20);

}

// decelerate from maximum speed to zero

for (int i = 255; i >= 0; --i)

{

analogWrite(enA, i);

analogWrite(enB, i);

delay(20);

}

// now turn off motors

digitalWrite(in1, LOW);

digitalWrite(in2, LOW);

digitalWrite(in3, LOW);

digitalWrite(in4, LOW);

}

void loop()

{

demoOne();

delay(1000);

demoTwo();

delay(1000);

}