This summer I decided to have some fun with the various robotics we have at our school. The first one I worked with was the Arduino microcontroller. I bought a couple of different quicks from an offshore website (https://www.banggood.com/4WD-Smart-Robot-Car-Chassis-Kits-With-Strong-Magneto-Speed-Encoder-p-917007.html, https://www.banggood.com/2WD-Avoidance-Tracking-Smart-Robot-Car-Chassis-Kit-With-Speed-Encoder-Ultrasonic-For-Arduino-UNO-R3-p-1124282.html) that took a few weeks to arrive, but came with almost all the parts needed (extra wiring was not included, and for the second car I wanted the L298N Motor controller).
It was somewhat tricky to get started, I do not have a background in wiring and electrical work (have wired one Arduino before with a very detailed video) on microcontrollers but this one was not too difficult and has a lot of similar videos that can help.
The trickiest part of the build was linking up the motors to the motor controller and then linking that to the Arduino (or the motor shield, which I have done both now). The kits do need additional power supplies (the only downside to using the motor control), so I am currently waiting for the 9V battery adapters from Amazon before I can let them drive around my garage freely.
I learned that the fear I had with wiring it wrong was not one I should worry about. The forgiving part of the arduino is that as long as you identify it correctly in the code as a global variable to the correct pin it will work. You will need to also connect the ground to the motor and the arduino board ground (on the port side). The motor controller is beneficial because you can use it to combine motors, change speeds, and provide it with additional power to make it function correctly. The first car I made had only one power supply to a breadboard and it was unable to work correctly. Everytime it was connected to the PC is would drive at the right speed and then when connected to battery it would go much faster.
I found the one thing missing from the readings I found was that it did not explain the controllers on the motors easily, so I am labeling it here. Using L298N motor:
– ENA/b controls the speed
– IN1 right motor forward movement if set to high (IN2 must be set to low)
– IN2 right motor reverse movement if set high (IN1 must be set to low)
– IN1 left motor forward movement if set to high (IN4 must be set to low)
– IN2 left motor reverse movement if set high (IN3 must be set to low)
– setting all motors to low will turn it off (like using breaks)
here is the code I have used.
// motor one
int enA = 10;
int in1 = 9;//for
int in2 = 8;//rev
// motor two
int enB = 5;
int in3 = 7;//for
int in4 = 6;//rev
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 demoThree()
{
//left turn
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, 200);
analogWrite(enA, 200);
delay(1000);
//right turn
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enB, 200);
analogWrite(enA, 200);
}
void loop()
{
demoOne();
delay(1000);
demoThree();
delay(1000);
}
Attached are some photos as well of the designs. I will post again on this when I have them moving freely. And I will post about the utlrasonic sensor as well.