Beruflich Dokumente
Kultur Dokumente
July 2018
Search for “DIY L298N 2WD” on http://www.banggood.com.
1 Mount the Swivel Wheel
Parts Needed
Swivel Wheel
4 × 5 mm M3 screws
4 × M3 nuts
2 Determine the top of the baseplate; it is not symmetric. Use the Arduino as a guide.
2 Attach the swivel to the bottom of the baseplate using the screws and nuts.
2 Mount the Arduino and Sensor Shield
Parts Needed
Arduino
Sensor Shield
2 × 10 mm M3 spacers
4 × 5 mm M3 screws
2 Connect the two spacers to the top of the baseplate using two screws
2 Mount the Arduino on the spacers using the other two screws
2 Mount the Sensor Shield atop the Arduino. Line up the right edges of the boards.
Make sure each pin goes in a connector.
3 Mount the Motors on the Bottom of the Baseplate
Parts Needed
2 × motors
2 × 10 cm wires, black
2 × 10 cm wires, red
4 × acrylic tabs
4 × 30 mm M3 screws
4 × M3 nuts 2 × Wheels
2 Solder one red and one black wire to each motor. Either terminal can be either color.
2 Place one tab in a slot down from the top of the baseplate, align the motor, and place
the other tab on the slot on the side of the baseplate.
2 Insert two screws from the outside tab through the motor and inside tab and connect
the nuts.
Rocker switch
L298N board
Battery holder
3 × 5 mm M3 screws
2 × 8 mm countersunk M3 screws
2 × 10 mm M3 spacers
1 × M3 nut
2 Insert the switch on the top of the baseplate between the two motors
2 Locate the battery holder on the underside of the baseplate just in front of the swivel
and insert the two countersuck M3 screws into the leftmost and rightmost holes.
2 Connect one of the screws to the M3 nut and the other to a spacer
2 Mount the L298N board to the two protruding spacers with the two other 5 mm
screws.
5 Download the Firmware to Center the Servo
Parts Needed
Computer
Servo Motor
robot-car sketch
USB cable
2 Plug the servo motor connector into the column of pins labeled “11” on the Sensor
Shield. Put the orange wire in the “S” row so the brown wire goes in the “G” row.
2 Connect the USB cable between your computer and the Arduino
2 Make sure a port is selected under Tools → Port
2 Click on the “upload” button to compile and send your sketch to the Arduino
2 Disconnect the USB cable once the program starts and centers the servo.
2 Disconnect the servo connection from the Sensor Shield; we will re connect it later.
6 Assemble the Ultrasonic Sensor Mount
Parts Needed
2 Trim the two arms so they have only 3 holes and their ends are narrow enough to fit
in the base.
2 Fit the servo arm into the base and attach it with the two 3 mm self-tapping screws.
2 Strap the ultrasonic sensor, pins up, to the mount with two twist ties looped around
the two transducers
7 Attach the Ultrasonic Sensor to the Baseplate
Parts Needed
4 × 11 mm M1 screws
4 × M1 nuts
2 Attach the base of the ultrasonic sensor to the top of the baseplate with the screws
and nuts.
8 Connect the power and motor wires
Parts Needed
2 Solder the red wire from the battery holder to one switch terminal
2 Solder the 20 cm red wire to the other switch terminal
2 Connect the two left motor wires to the left connector on the L298N and the two right
motor wires to the right connector. Red and black do not matter here; reverse them if
the robot goes backwards.
2 Connect two 12 cm lengths of wire to the black (ground) wire from the battery holder
and the red (power) wire from the power switch.
2 Connect the doubled black (ground) wire to the “GND” terminal on the shield.
2 Connect the doubled red (power) wire to the “VCC” terminal on the shield.
2 Connect the black 12 cm wire from the shield terminal to the middle screw terminal
on the L298N.
2 Connect the red 12 cm wire from the shield terminal to the left screw terminal on the
L298N.
9 Connect the Ultrasonic Sensor, Servo, and L298N to the Arduino Sensor Shield
Parts Needed
2 After centering the servo, the robot turns its left wheel forward then backward, then
its right wheel forward then backward.
If a wheel turns backwards, reverse its motor’s connection to the L298N board (swap
the red and black motor wires).
If one wheel turns but not the other, check the motor wiring and the wiring between
the Arduino and the L298N.
2 Disconnect the robot from the computer, turn off the power switch, and plug in 4 AA
batteries to the holder on the bottom. Turn on the power switch.
Again, you should see power lights on all three boards. If not, check the wiring
around the battery holder and the switch.
11 Software
Below and on the next few pages is the “robot-car.ino” sketch (software) that operates the
car. This initial code indicates we will be using a servo motor and defines the pin numbers
for the ultrasonic module, the servo motor, and the L298N motor controller.
/*
* Firmware for the ”2WD Ultrasonic Motor Robot Car Kit”
*
* Stephen A. Edwards
*
* Hardware configuration :
* A pair of DC motors driven by an L298N H bridge motor driver
* An HC−SR04 ultrasonic range sensor mounted atop a small hobby servo
*/
#include <Servo.h>
Servo servo ;
// Set motor speed: 255 full ahead, −255 full reverse , 0 stop
void go( enum Motor m, int speed)
{
digitalWrite (m == LEFT ? in1Pin : in3Pin , speed > 0 ? HIGH : LOW );
digitalWrite (m == LEFT ? in2Pin : in4Pin , speed <= 0 ? HIGH : LOW );
analogWrite(m == LEFT ? enAPin : enBPin, speed < 0 ? −speed : speed );
}
To check your motors work and are not wired backwards, the testMotors function cycles
the left motor forward then back, briefly passing through 50% speed (128) on the way, then
repeats this for the right motor.
// Initial motor test :
// left motor forward then back
// right motor forward then back
void testMotors ()
{
static int speed[8] = { 128, 255, 128, 0 ,
−128, −255, −128, 0};
go(RIGHT, 0);
// Scan the area ahead by sweeping the ultrasonic sensor left and right
// and recording the distance observed. This takes a reading , then
// sends the servo to the next angle . Call repeatedly once every 50 ms or so .
void readNextDistance ()
{
static unsigned char angleIndex = 0;
static signed char step = 1;
pinMode(enAPin, OUTPUT);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);
pinMode(enBPin, OUTPUT);
go(LEFT, 0);
go(RIGHT, 0);
testMotors ();
if (tooClose) {
// Something's nearby: back up left
go(LEFT, −180);
go(RIGHT, −80);
} else {
// Nothing in our way: go forward
go(LEFT, 255);
go(RIGHT, 255);
}