7. Motor_with_transistor
The examples below use definition blocks to better organize the code.
7.1. Connections
7.2. 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.
7.3. Write digital
pin0.write_digital(1)
.pin0.write_digital(0)
.7.4. Turn on and off pin0
from microbit import *
def turn_on():
pin0.write_digital(1)
def turn_off():
pin0.write_digital(0)
while True:
if button_a.is_pressed():
turn_on()
elif button_b.is_pressed():
turn_off()
sleep(500)
Tasks
Use the turn on and turn off functions to complete these 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 *
def turn_on():
pin0.write_digital(1)
def turn_off():
pin0.write_digital(0)
while True:
turn_on()
sleep(6000)
turn_off()
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 *
def turn_on():
pin0.write_digital(1)
def turn_off():
pin0.write_digital(0)
while True:
if button_a.is_pressed():
turn_on()
sleep(6000)
turn_off()
sleep(2000)
elif button_b.is_pressed():
turn_on()
sleep(2000)
turn_off()
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 *
def turn_on():
pin0.write_digital(1)
def turn_off():
pin0.write_digital(0)
while True:
if button_a.is_pressed():
turn_on()
sleep(6000)
turn_off()
sleep(2000)
elif button_b.is_pressed():
turn_on()
sleep(2000)
turn_off()
sleep(6000)
else:
turn_on()
sleep(4000)
turn_off()
sleep(4000)
Tasks
Write a new function that uses the turn_on and turn-off functions and has parameters for the length of time to turn it on and the length of time to turn it off. Set both of these parameters to a default value of 4000.
Write code, using this new function, 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 a new function that uses the turn_on and turn-off functions and has parameters for the length of time to turn it on and the length of time to turn it off. Set both of these parameters to a default value of 4000.
from microbit import *
def turn_on_off(time_on=4000, time_off=4000):
turn_on()
sleep(time_on)
turn_off()
sleep(time_off)
Write code, using this new function, 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 *
def turn_on():
pin0.write_digital(1)
def turn_off():
pin0.write_digital(0)
def turn_on_off(time_on=4000, time_off=4000):
turn_on()
sleep(time_on)
turn_off()
sleep(time_off)
while True:
if button_a.is_pressed():
turn_on_off(time_on=6000, time_off=2000)
elif button_b.is_pressed():
turn_on_off(time_on=2000, time_off=6000)
else:
turn_on_off()
7.5. 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 *
def 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)
def 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)
while True:
if button_a.is_pressed():
pulse_on()
elif button_b.is_pressed():
pulse_off()
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 and pulse_off functions further to show the motor speed as a value from 0 to 9. Hint: write a new function,
motor_speed_display
, that uses the int function to round down the value obtained from dividing the motor speed by 100. Then use the min function to make sure the highest possible value returned is 9.
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 *
def 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)
def 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)
while True:
if button_a.is_pressed():
pulse_on()
elif button_b.is_pressed():
pulse_off()
sleep(500)
7.6. 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 *
def motor_speed_display(analog_val):
display.show(min(9, int(analog_val / 100)))
def pulse_on():
sleep_time = 500
step_size = 100
for i in range(200, 1023, step_size):
pin0.write_analog(i)
motor_speed_display(i)
sleep(sleep_time)
pin0.write_analog(1023)
motor_speed_display(1023)
def pulse_off():
sleep_time = 250
step_size = 200
for i in range(1023, 200, -step_size):
pin0.write_analog(i)
motor_speed_display(i)
sleep(sleep_time)
pin0.write_analog(0)
motor_speed_display(0)
while True:
if button_a.is_pressed():
pulse_on()
elif button_b.is_pressed():
pulse_off()
sleep(500)