Micro:bit Gesture-Controlled Car
by AnasOsama07 in Circuits > Remote Control
2289 Views, 12 Favorites, 0 Comments
Micro:bit Gesture-Controlled Car
This is a project created for participation in the micro:bit Contest and since the only things I don't have were micro:bits, so I've decided to build the project on Tinkercad. Another reason that made me think about this idea, is that I've already created a project like this one using the Arduino Uno, an MPU-6050, and the same components used in this project so I tried to recreate it using micro:bit instead of Arduino, and I hope you like my project.
Supplies
- micro:bit ×1
For the car :
Getting the Controller Ready
At first, you must upload the controller's code.
Code in python:
sensitivity = 215 #This variable stores the sensitivity of the controller and the greater its value, the lower its sensitivity will be.
radio.set_group(5)
#In the forever loop, the if statements check the tilt direction of the controller
def on_forever():
if input.acceleration(Dimension.Y) >= sensitivity and input.acceleration(Dimension.Y) > input.acceleration(Dimension.X): #if the controller is tilted forward
basic.show_arrow(ArrowNames.North)
radio.send_number(1) #move the car forward
else:
if input.acceleration(Dimension.Y) <= 0 - sensitivity and input.acceleration(Dimension.Y) < input.acceleration(Dimension.X): # if the controller is tilted backward
basic.show_arrow(ArrowNames.South)
radio.send_number(2) #move the car backward
else:
if input.acceleration(Dimension.X) >= sensitivity and input.acceleration(Dimension.X) > input.acceleration(Dimension.Y): #if the controller is tilted to the right
basic.show_arrow(ArrowNames.East)
radio.send_number(3) #turn the car right
else:
if input.acceleration(Dimension.X) <= 0 - sensitivity and input.acceleration(Dimension.X) < input.acceleration(Dimension.Y): #if the controller is tilted to the left
basic.show_arrow(ArrowNames.West)
radio.send_number(4) #turn the car left
else: #if there's no tilt direction detected
basic.show_icon(IconNames.No)
radio.send_number(0) #stop the car
basic.forever(on_forever)Downloads
Getting the Car's Micro:bit Ready
Next, upload the sketch below to one of the microbits and then you need to connect it to the breakout board and then connect it to a breadboard.
Note: connecting the car's micro:bit to a breadboard is optional if you plan to use something else for wiring.
Code in Python:
value = 0 #This variable stores the value received by radio
radio.set_group(5)
# The forever loop is empty since we don't need it in our car
def on_forever():
pass
basic.forever(on_forever)
# This function starts moving the car when the controller sends the direction.
def on_radio_received_number(arg0):
global value
value = arg0
# move the car forward
if value == 1:
basic.show_arrow(ArrowNames.North)
pins.digital_write_pin(DigitalPin.P2, 1)
pins.digital_write_pin(DigitalPin.P0, 1)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P11, 1)
pins.digital_write_pin(DigitalPin.P5, 1)
pins.digital_write_pin(DigitalPin.P8, 0)
else:
# move the car backward
if value == 2:
basic.show_arrow(ArrowNames.South)
pins.digital_write_pin(DigitalPin.P2, 1)
pins.digital_write_pin(DigitalPin.P0, 0)
pins.digital_write_pin(DigitalPin.P1, 1)
pins.digital_write_pin(DigitalPin.P11, 1)
pins.digital_write_pin(DigitalPin.P5, 0)
pins.digital_write_pin(DigitalPin.P8, 1)
else:
# turn the car right
if value == 3:
basic.show_arrow(ArrowNames.East)
pins.digital_write_pin(DigitalPin.P2, 1)
pins.digital_write_pin(DigitalPin.P0, 0)
pins.digital_write_pin(DigitalPin.P1, 1)
pins.digital_write_pin(DigitalPin.P11, 1)
pins.digital_write_pin(DigitalPin.P5, 1)
pins.digital_write_pin(DigitalPin.P8, 0)
else:
# turn the car left
if value == 4:
basic.show_arrow(ArrowNames.West)
pins.digital_write_pin(DigitalPin.P2, 1)
pins.digital_write_pin(DigitalPin.P0, 1)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P11, 1)
pins.digital_write_pin(DigitalPin.P5, 0)
pins.digital_write_pin(DigitalPin.P8, 1)
else:
# stop the car's motion
basic.show_icon(IconNames.No)
pins.digital_write_pin(DigitalPin.P2, 0)
pins.digital_write_pin(DigitalPin.P0, 0)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P11, 0)
pins.digital_write_pin(DigitalPin.P5, 0)
pins.digital_write_pin(DigitalPin.P8, 0)
radio.on_received_number(on_radio_received_number)Downloads
Adding External Power and the Motors
If you're using a breadboard for wiring, connect the 9V battery with one of the power busses/rails and then connect the negative rail to the GND pin of the car's micro:bit, and then connect the motors to their wheels and add them to the breadboard as well but make sure you connect them the same way as I did and they don't have to be in the same places.
[Click on the photo above to see how] or [if you don't want to open the photo, then both the motors on the left are connected to each other, but the bottom one must be different and you'll have to connect it's negative pin as the positive and its positive as its negative, and the same thing applies to the motors on the right. The reason is that if you connect both pins the right way, one motor will spin in the wrong direction and we adjust this by doing the previous explaination]
Adding the H-Bridge
Here we're adding the H-Bridge motor driver to our circuit and we connect the power to it so that it could operate the motors as well as connecting the motors. The connections are in the picture above.
Connecting the Micro:bit to the H-Bridge Motor Controller
Connections:
Pin 2 : Right Motors Speed Control
Pin 0 : Right Motors Forward Direction Control
Pin 1 : Right Motors Backward Direction Control
Pin 11 : Left Motors Speed Control
Pin 5 : Left Motors Forward Direction Control
Pin 8 : Left Motors Backward Direction Control
Done!