Introduction
The first thing that you'll want to do with a robot vehicle is make
it move. To do this we need to send the correct electrical signals to
the motors.
Programming
The motors are connected to the micro:bit on the following pins,
- LFT Motor: PWM pin 0, DIR pin 8
- RGT Motor: PWM pin 1, DIR pin 12
When you want to drive the motors, you start by using write_digital() on the IDR (direction pins) with a 0 for forwards and a 1 for backwards. You then use write_analog() on the PWM pins to set the speed of the motor.
For example, to go forwards at full speed for 2 seconds and then stop, you would write,
pin8.write_digital(0)
pin12.write_digital(0)
pin0.write_analog(1023)
pin1.write_analog(1023)
sleep(2000)
pin0.write_analog(0)
pin1.write_analog(0)
You can write a value from 0 to 1023 in the write_analog statements.
The higher your value, the quicker you move forwards. This would be a
little under 80% speed.
pin8.write_digital(0)
pin12.write_digital(0)
pin0.write_analog(800)
pin1.write_analog(800)
To reverse, first write a 1 to the direction pins of each motor. Then
write a value from 0 to 1023 like before. This time, however, the
smaller your number, the quicker you go backwards. Subtract your speed
from 1023 to get the value you write. If we want to reverse at '800',
the same speed backwards as we just went forwards, we would write,
pin8.write_digital(1)
pin12.write_digital(1)
pin0.write_analog(223)
pin1.write_analog(223)
If the value you write_analog() with is not the same for each motor,
then the robot will turn in the direction of the motor going the
slowest. If the difference between the two values is small, then the
turn is gentle. If you have a large difference between the values, you
get a sharper turn.