5. Motor_with_transistor
5.1. Model
Place the resistor and transistor first.
Make sure the transistor front flat edge is facing forwards.
Connect with the motor terminal block.
Connect with the jumper wires.
5.2. Write digital
pin0.write_digital(1)
.pin0.write_digital(0)
.5.3. Turn on and off pin0
from microbit import *
while True:
if button_a.is_pressed():
pin0.write_digital(1)
elif button_b.is_pressed():
pin0.write_digital(0)
sleep(500)
Tasks
Write code to turn on the motor for 6 seconds then turn it off for 2 seconds before repeating.
Write code to turn on the motor for 6 seconds then turn it off for 2 seconds when the A-button is pressed, and on for 2 seconds then off for 6 seconds when the B-button is pressed, and off when nothing is pressed.
Write code to turn on the motor for 6 seconds then turn it off for 2 seconds when the A-button is pressed, and on for 2 seconds then off for 6 seconds when the B-button is pressed, and on for 4 seconds then off for 4 seconds when nothing is pressed.
Write code to turn on the motor for 6 seconds then turn it off for 2 seconds before repeating.
from microbit import *
while True:
pin0.write_digital(1)
sleep(6000)
pin0.write_digital(0)
sleep(2000)
Write code to turn on the motor for 6 seconds then turn it off for 2 seconds when the A-button is pressed, and on for 2 seconds then off for 6 seconds when the B-button is pressed, and leave it off when nothing is pressed.
from microbit import *
while True:
if button_a.is_pressed():
pin0.write_digital(1)
sleep(6000)
pin0.write_digital(0)
sleep(2000)
elif button_b.is_pressed():
pin0.write_digital(1)
sleep(2000)
pin0.write_digital(0)
sleep(6000)
Write code to turn on the motor for 6 seconds then turn it off for 2 seconds when the A-button is pressed, and on for 2 seconds then off for 6 seconds when the B-button is pressed, and on for 4 seconds then off for 4 seconds when nothing is pressed.
from microbit import *
while True:
if button_a.is_pressed():
pin0.write_digital(1)
sleep(6000)
pin0.write_digital(0)
sleep(2000)
elif button_b.is_pressed():
pin0.write_digital(1)
sleep(2000)
pin0.write_digital(0)
sleep(6000)
else:
pin0.write_digital(1)
sleep(4000)
pin0.write_digital(0)
sleep(4000)
5.4. Write analog
pin0.write_analog(1023)
.pin0.write_analog(0)
.write_analog
can have values from 0 to 1023.write_analog
can be used to power the motor at different speeds.write_analog
to change the speed of the motor.from microbit import *
import random
while True:
if button_a.is_pressed():
# pulse on
sleep_time = 500
step_size = 200
for i in range(200, 1023, step_size):
pin0.write_analog(i)
sleep(sleep_time)
pin0.write_analog(1023)
elif button_b.is_pressed():
# pulse off
sleep_time = 500
step_size = 200
for i in range(1023, 200, -step_size):
pin0.write_analog(i)
sleep(sleep_time)
pin0.write_analog(0)
sleep(500)
Tasks
Modify the pulse_on code so it has twice as many steps. Modify the pulse_off code so each step is half as long.
Modify the pulse_on code so it has twice as many steps. Modify the pulse_off code so each step is half as long.
from microbit import *
import random
while True:
if button_a.is_pressed():
# pulse on
sleep_time = 500
step_size = 100
for i in range(200, 1023, step_size):
pin0.write_analog(i)
sleep(sleep_time)
pin0.write_analog(1023)
elif button_b.is_pressed():
# pulse off
sleep_time = 250
step_size = 200
for i in range(1023, 200, -step_size):
pin0.write_analog(i)
sleep(sleep_time)
pin0.write_analog(0)
sleep(500)
5.5. Scaling the readings
- min(9, int(analog_val / 100))
Converts an analog value from a range of 0 to 1023 to 0 to 9.
e.g. scaled_value = min(9, int(analog_val / 100))
Tasks
Modify the pulse_on and pulse_off code to show the motor speed as a value from 0 to 9.
Modify the pulse_on and pulse_off code to show the motor speed as a value from 0 to 9.
from microbit import *
import random
while True:
if button_a.is_pressed():
# pulse on
sleep_time = 500
step_size = 100
for i in range(200, 1023, step_size):
pin0.write_analog(i)
display.show(min(9, int(i / 100)))
sleep(sleep_time)
pin0.write_analog(1023)
display.show(min(9, int(1023 / 100)))
elif button_b.is_pressed():
# pulse off
sleep_time = 250
step_size = 200
for i in range(1023, 200, -step_size):
pin0.write_analog(i)
display.show(min(9, int(i / 100)))
sleep(sleep_time)
pin0.write_analog(0)
display.show(min(9, int(0 / 100)))
sleep(500)