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. Write digital

To turn the motor on fully, use pin0.write_digital(1).
To turn the motor off, use pin0.write_digital(0).

5.3. 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.4. Write analog

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

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

The analog range, from 0 to 1023, can roughly be converted to a scaled range of 0 to 9 by dividing by 100.
The expression below uses int to round down to an integer and min to keep the maximum value at 9.
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

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