Introduction¶
In this tutorial, we will guide you through the process of setting up and programming a remote-controlled robot using an Arduino. The robot will be controlled using an IBus receiver and transmitter. We will simplify the setup by removing additional sensors and LEDs to focus solely on motor control via the remote.
Materials Needed¶
- Arduino (e.g., Arduino Uno)
- Motor Driver
- IBus Receiver and Transmitter
- DC Motors (x2)
- Battery pack
- Jumper wires
- Breadboard (optional)
Wiring¶
Here's how you should wire your components:
- Motors to Motor Driver:
- Motor 1: Connect one motor to the output terminals of the first VNH3SP30 motor driver.
-
Motor 2: Connect the second motor to the output terminals of the second VNH3SP30 motor driver.
-
Motor Drivers to Arduino:
- Motor Driver 1:
- PWM Pin: Connect to Arduino pin 9
- INA Pin: Connect to Arduino pin 8
- INB Pin: Connect to Arduino pin 7
-
Motor Driver 2:
- PWM Pin: Connect to Arduino pin 3
- INA Pin: Connect to Arduino pin 5
- INB Pin: Connect to Arduino pin 4
-
IBus Receiver to Arduino:
- Connect the IBus signal pin to the Arduino's RX pin.
- Connect the IBus power and ground pins to the Arduino's 5V and GND pins, respectively.
Important: To program the Arduino, you must disconnect the IBus receiver from the RX pin to avoid interference during uploading.
Programming the Arduino¶
- Open the Arduino IDE on your computer.
- Copy and paste the following code into the IDE:
#include <Wire.h>
#include <VNH3SP30.h>
#include <IBusBM.h>
// Define motor control pins
#define M1_PWM 9 // pwm pin motor
#define M1_INA 8 // control pin INA
#define M1_INB 7 // control pin INB
#define M2_PWM 3 // pwm pin motor
#define M2_INA 5 // control pin INA
#define M2_INB 4 // control pin INB
VNH3SP30 Motor1; // Define control object for motor 1
VNH3SP30 Motor2; // Define control object for motor 2
IBusBM IBus; // IBus object for receiving signals from transmitter/receiver
void setup() {
Wire.begin();
Serial.begin(9600);
Serial.println("\r\nBens Bot");
Serial.println("By Tim Schumann");
Serial.println();
Motor1.begin(M1_PWM, M1_INA, M1_INB, -1, -1); // Motor 1 object connected through specified pins
Motor2.begin(M2_PWM, M2_INB, M2_INA, -1, -1); // Motor 2 object connected through specified pins
IBus.begin(Serial); // IBus connected to Serial1: 1 (RX)
// We have to wait for the receiver to receive data from the transmitter (transmitter needs to be turned on)
Serial.println("Wait for receiver");
while (IBus.cnt_rec == 0) delay(100);
Serial.println("Init done");
}
void loop() {
int spd, turn;
// Speed depends on front switch (channel 5) (forward/backwards) and channel 2 (speed)
spd = ((int)IBus.readChannel(1) - 1500);
// Every value below 1050 we interpret as stop
if (spd < 50 && spd > -50) spd = 0;
else spd = (spd * 4) / 15; // Value could reach (2000-1050)*4/9 = 422, but setSpeed() will max at 400
if (IBus.readChannel(5) > 1500) spd = -spd; // Backward/forward depends on switch at channel 6
// Turn depends on channel 0, scale down to -200, +200
turn = -(((int)IBus.readChannel(3) - 1500) * 4) / 10;
if (turn > -20 && turn < 20) turn = 0;
// Set combined speed and turn (if speed==0, then only turn in place)
speedturn(spd, turn);
delay(10);
}
void setSpeed(int speed) {
Motor1.setSpeed(speed);
Motor2.setSpeed(speed);
}
void brake(int brakePower) {
Motor1.brake(brakePower);
Motor2.brake(brakePower);
}
void turn(int angle) {
// Turn vehicle by providing different speed settings to the motors.
// Angle can be positive (right turn) or negative (left turn).
// If the vehicle is already stopped, the vehicle will turn in place.
int currentSpeed = (Motor1.speed + Motor2.speed) / 2;
Motor1.setSpeed(currentSpeed + angle);
Motor2.setSpeed(currentSpeed - angle);
}
void speedturn(int speed, int angle) {
// Set speed (-400 -> +400) and turn (-400 -> +400)
// Turn vehicle by providing different speed settings to the motors.
// Angle can be positive (right turn) or negative (left turn).
// If the vehicle is already stopped, the vehicle will turn in place.
Motor1.setSpeed(speed + angle);
Motor2.setSpeed(speed - angle);
}
- Upload the code to your Arduino (remember to disconnect the IBus receiver first).
- Reconnect the IBus receiver after the code is successfully uploaded.
Additional Task¶
To help you understand the code better and make small modifications, here are some tasks you can try:
- Change the motor speed scaling: Adjust the scaling factor for the motor speed in the code.
Hint
Look for the line that scales the speed in the `loop` function.Solution
Replace `spd = (spd * 4) / 15;` with `spd = (spd * 3) / 10;` to change the scaling factor.- Add a brake function: Implement a function that stops the motors when a specific channel value is read. -> Arms switch
Hint
You will need to use the `brake` function in the `loop` function.Solution
Add the following code to the `loop` function: ```cpp if (IBus.readChannel(4) > 1500) { // Brake switch //previous code }else{ brake(400); // Maximum brake power } ```- Modify the turn sensitivity: Adjust the sensitivity of the turning mechanism.
Hint
Look for the line that scales the turn value in the `loop` function.Solution
Replace `turn = -(((int)IBus.readChannel(3) - 1500) * 4) / 10;` with `turn = -(((int)IBus.readChannel(3) - 1500) * 3) / 10;` to make the turning less sensitive.By following this tutorial, you should have a functional remote-controlled robot and a better understanding of how to program and modify it. Enjoy building and experimenting with your robot!