5. Motor_with_transistor

The examples below use sequence without definition blocks which are preferred to better organize the code.
The motor requires a 2.2 k ohm resistor with the transistor to power it properly using code.
The 2.2 k ohm resistor has Red, Red, Red, Gold coloured bands.
../_images/2.2kohm.png

5.1. Model

  1. Place the resistor and transistor first.

  2. Make sure the transistor front flat edge is facing forwards.

  3. Connect with the motor terminal block.

  4. Connect with the jumper wires.

../_images/motor_1b_bb.png ../_images/motor_2b_bb.png ../_images/motor.jpg

5.2. Model: Moved to side for better terminal block attachment

  1. Place the resistor and transistor first.

  2. Make sure the transistor front flat edge is facing forwards.

  3. Connect with the motor via the pins at the end of wires.

  4. Connect with the jumper wires.

../_images/motor_1c_bb.png ../_images/motor_2c_bb.png

5.3. Model: Motor without terminal block

  1. Place the resistor and transistor first.

  2. Make sure the transistor front flat edge is facing forwards.

  3. Connect with the motor via the pins at the end of wires.

  4. Connect with the jumper wires.

../_images/motor_1d_bb.png ../_images/motor_2d_bb.png

5.4. Write digital

pinx.write_digital(value)
pinx is the pin. e.g pin0, pin1, pin2.
value is 1 for on and 0 for off.
To turn the motor on fully, use pin0.write_digital(1).
To turn the motor off, use pin0.write_digital(0).

5.5. Turn on and off pin0

Pressing A turns on the motor.
Pressing B turns off the motor.
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

  1. Write code to turn on the motor for 6 seconds then turn it off for 2 seconds before repeating.

  2. 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.

  3. 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)
pinx is the pin. e.g pin0, pin1, pin2.
value is an integer from 0 to 1023.
To turn the motor on fully use pin0.write_analog(1023).
To turn the LED off use 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.
Here is some sample code making use of a list and 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

  1. Modify the code so it has 3 power levels: 0, 200, 1023.

  2. 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)

Here is some sample code making use of the range function and write_analog to change the speed of the motor.
It starts with a value of 100, and goes up in 10 steps of 20.
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

  1. Modify the code so it starts at 50 and speeds up in 5 steps of 50.

  2. 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)

Here is some sample code making use of the range function and 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)