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. Model: Moved to side for better terminal block attachment
Place the resistor and transistor first.
Make sure the transistor front flat edge is facing forwards.
Connect with the motor via the pins at the end of wires.
Connect with the jumper wires.
5.3. Model: Motor without terminal block
Place the resistor and transistor first.
Make sure the transistor front flat edge is facing forwards.
Connect with the motor via the pins at the end of wires.
Connect with the jumper wires.
5.4. Write digital
- pinx.write_digital(value)
pinxis the pin. e.g pin0, pin1, pin2.valueis 1 for on and 0 for off.
pin0.write_digital(1).pin0.write_digital(0).5.5. 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.6. Write analog
- pinx.write_analog(value)
pinxis the pin. e.g pin0, pin1, pin2.valueis an integer from 0 to 1023.
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
power_levels = [0, 50, 100, 200, 400, 1023]
while True:
if button_a.is_pressed():
for power in power_levels:
pin0.write_analog(power)
sleep(1000)
sleep(500)
Tasks
Modify the code so it has 3 power levels: 0, 200, 1023.
Modify the code so it has 3 power levels: 200, 500, 1023, but the motor is stopped for 500ms between each.
Modify the code so it has 3 power levels: 0, 200, 1023.
from microbit import *
import random
power_levels = [0, 200, 1023]
while True:
if button_a.is_pressed():
for power in power_levels:
pin0.write_analog(power)
sleep(1000)
sleep(500)
Modify the code so it has 3 power levels: 200, 500, 1023, but the motor is stopped for 500ms between each.
from microbit import *
import random
power_levels = [200, 500, 1023]
while True:
if button_a.is_pressed():
for power in power_levels:
pin0.write_analog(power)
sleep(1000)
pin0.write_analog(0)
sleep(1000)
sleep(500)
write_analog to change the speed of the motor.from microbit import *
sleep_time = 500
start_val = 100
while True:
if button_a.is_pressed():
# increase speed
for i in range(10):
pin0.write_analog(start_val + 20 * i)
sleep(sleep_time)
pin0.write_analog(0)
sleep(1000)
Tasks
Modify the code so it starts at 50 and speeds up in 5 steps of 50.
Modify the code so it starts at 320 and slows down in 4 steps of 80.
Modify the code so it starts at 50 and speeds up in 5 steps of 50.
from microbit import *
sleep_time = 500
start_val = 50
while True:
if button_a.is_pressed():
# increase speed using i = 0 to 5
for i in range(6):
pin0.write_analog(start_val + 50 * i)
sleep(sleep_time)
pin0.write_analog(0)
sleep(1000)
Modify the code so it starts at 320 and slows down in 4 steps of 80.
from microbit import *
sleep_time = 500
start_val = 320
while True:
if button_a.is_pressed():
# decrease speed using i = 0 to 4
for i in range(5):
pin0.write_analog(start_val - 80 * i)
sleep(sleep_time)
pin0.write_analog(0)
sleep(1000)
write_analog to change the speed of the motor in an attempt to find values ot just get the motor spinning.from microbit import *
import random
power_level_start = 140
power_step = 2
while True:
if button_a.is_pressed():
for power_rep in range(20):
power = power_level_start + power_step * power_rep
display.scroll(power)
pin0.write_analog(power)
sleep(1000)
pin0.write_analog(0)
sleep(500)
sleep(500)