

Add an else condition for when the robot is just moving forward. We then stop, print another message to the shell before turning left at 75% power for one second.

Then it will use the reverse function at 75% power for one second. On bumping, it will first print a message to the Python shell. Write code for what happens if the robot bumps into something. If the robot has bumped into an object, then the switch is closed, connecting the 3V terminal to GP0 and changing the value from 0 to 1. This test will check the GPIO pin used for the bumper microswitch. Create a while True loop to continuously run the code. This delay will give us the time to place the robot on the floor and move out of its way. Create a final function to stop the robot. For this we need the motors to work in opposing directions so that the robot turns on the spot. Create two more functions for moving the robot left and right.

Create another function for reverse control. But only change one set of values, for example the forward function, and test before moving on. if the “f” and “r” values don’t work for you then swap them. The second line is almost identical, but with Motor 2 specified and the direction of travel set to reverse, as the motor will be seated on the opposite side of the robot. We specify the direction of travel as forwards “f”, and the speed variable is passed when the function is called. The first controls the motor connected to Motor 1. Inside the function we have two lines of code. This function has one argument and that is the speed at which we want the robot to move. board = PicoMotorDriver.KitronikPicoMotor()įorward_bumper = machine.Pin(0,, _DOWN)ħ. We set this pin as an input, and ensure that the pin is pulled low (0V). The second, “forward_bumper” creates a connection between the driver board and the microswitch. The first, “board” is used to enable control of the motor driver board. PicoMotorDriver is the library that we just created and utime is used to pause and control the timings necessary for controlling the robot. Create a new file for the project code.ĥ. Save the file as PicoMotorDriver.py to your Raspberry Pi Pico.Ĥ. Create a new file in Thonny and paste the text. Copy the text of this motor driver library file.ģ. Press STOP to ensure that the Pico is connected.Ģ. Connect your Raspberry Pi Pico to your computer and open the Thonny application. Please note that it is important that you do not have the power switch set to ON while the driver board is connected to your computer as this may damage the Pico or your computer.ġ. Kitronik’s motor driver board has its own MicroPython library which abstracts the control of the motors and so we start by first installing that library. Secure the motor driver board and battery pack to the chassis.
