MicroSwitch Robot using the Cytron Maker Pi RP2040
This robot was inspired by my friend, Michael York.
Microswitches can be purchased for under $1. They can be mounted on the front of our robot. When the robot hits a wall in front of it the switch will open (or close) and the robot controller can make the robot go in reverse or turn.
In the example below, we attached a stiff wire to the lever of the microswitch.
In the example below, we connected three microswitches to the front of our robot.
If the left switch is activated, the robot should turn to the right. If the right switch is activated, the robot should go to the left.
This image shows how we used two of the Grove connectors to read in the values of the switches.
Testing Switches
The following code can be used to test your switches. A line on the console prints out which of the three switches are activated using the pin value() function.
frommachineimportPinfromtimeimportsleep# GPIO is the internal built-in LEDled0=Pin(0,Pin.OUT)led1=Pin(1,Pin.OUT)led2=Pin(2,Pin.OUT)# input on the lower left of the Pico using a built-in pull-down resistor to keep the value from floatingmiddle_switch=Pin(7,Pin.IN,Pin.PULL_DOWN)right_switch=Pin(28,Pin.IN,Pin.PULL_DOWN)left_switch=Pin(27,Pin.IN,Pin.PULL_DOWN)whileTrue:ifmiddle_switch.value():# if the value changesled0.on()print('middle')else:led0.off()ifright_switch.value():# if the value changesled1.on()print('right')else:led1.off()ifleft_switch.value():# if the value changesled2.on()print('left')else:led2.off()sleep(.1)
frommachineimportPin,PWMfromtimeimportsleep# GPIO is the internal built-in LEDled0=Pin(0,Pin.OUT)led1=Pin(1,Pin.OUT)led2=Pin(2,Pin.OUT)# input on the lower left of the Pico using a built-in pull-down resistor to keep the value from floatingmiddle_switch=Pin(7,Pin.IN,Pin.PULL_DOWN)right_switch=Pin(28,Pin.IN,Pin.PULL_DOWN)left_switch=Pin(27,Pin.IN,Pin.PULL_DOWN)# Go slow to avoid bending wiresPOWER_LEVEL=25000# max is 65000# These values depend on motor wiringRIGHT_FORWARD_PIN=10RIGHT_REVERSE_PIN=11LEFT_FORWARD_PIN=9LEFT_REVERSE_PIN=8right_forward=PWM(Pin(RIGHT_FORWARD_PIN))right_reverse=PWM(Pin(RIGHT_REVERSE_PIN))left_forward=PWM(Pin(LEFT_FORWARD_PIN))left_reverse=PWM(Pin(LEFT_REVERSE_PIN))defturn_motor_on(pwm):pwm.duty_u16(POWER_LEVEL)defturn_motor_off(pwm):pwm.duty_u16(0)defforward():turn_motor_on(right_forward)turn_motor_on(left_forward)turn_motor_off(right_reverse)turn_motor_off(left_reverse)defreverse():turn_motor_on(right_reverse)turn_motor_on(left_reverse)turn_motor_off(right_forward)turn_motor_off(left_forward)defturn_right():turn_motor_on(right_forward)turn_motor_on(left_reverse)turn_motor_off(right_reverse)turn_motor_off(left_forward)defturn_left():turn_motor_on(right_reverse)turn_motor_on(left_forward)turn_motor_off(right_forward)turn_motor_off(left_reverse)defstop():turn_motor_off(right_forward)turn_motor_off(right_reverse)turn_motor_off(left_forward)turn_motor_off(left_reverse)defmain():whileTrue:ifmiddle_switch.value():# if the value changesprint('middle')led0.on()reverse()sleep(1)turn_right()sleep(.75)forward()else:led0.off()forward()ifright_switch.value():# if the value changesprint('right')led1.on()reverse()sleep(.75)turn_left()sleep(.5)forward()else:led1.off()forward()ifleft_switch.value():# if the value changesled2.on()print('left')reverse()sleep(.75)turn_right()sleep(.5)forward()else:led2.off()forward()print('middle',middle_switch.value())print('left',left_switch.value())print('right',right_switch.value())try:main()exceptKeyboardInterrupt:print('Got ctrl-c')finally:# Optional cleanup codeprint('turning off sound')# sound_off()print('turning off motors')stop()