Pico Inventor's Kit Experiment 4 - Using a Transistor to Drive a Motor

Experiment 4 from the Inventors Kit for Raspberry Pi Pico, In which we explore using a transistor to drive a motor. Included in this resource are code downloads, a description of the experiment and also a video walk-through. This resource provides additional help and is not meant to replace the documentation that ships with the kit.

The Raspberry Pi Pico is a compact microcontroller based on the RP2040 processor - Raspberry Pi’s first in-house designed silicon. The RP2040 on the Pico uses 2 ARM processor cores, and also has 4 programmable IO controllers (PIO). These are like mini processors, and can help the Pico complete more complex tasks than it would otherwise be able to do. The Pico has 28 general purpose IO pins (GPIO), and 3 of these can also be used as Analogue inputs. The Pico can be programmed in several languages. The experiments in these resources are written in the Thonny Editor using MicroPython.

Pico Inventor's Kit Exp' 4 - Using a Transistor to Drive a Motor

The output pins on most microprocessors can only supply a small amount of current, not enough for a power hungry device such as a motor. The Pico is no exception to this.

A transistor can be used to solve this problem. A transistor is like a gate for electricity, a small amount of current can be used to open the gate to let a lot of current flow through to power hungry components.

  • To use a transistor to drive a fan motor.
  • To control the speed of the motor using Pulse Width Modulation (PWM).

Video Walk-through:

 

Exp 4 - The Code

All experiments are coded in MicroPython in Thonny. To open in Thonny, copy and past the code below directly into the Thonny editor.

'''
Kitronik Inventor's Kit Exp 4

Control motor speed via PWM control of a transistor.

All the Pico pins have PWM capability - in this experiment GP15 is used.
This is used to control the base of a transistor which actually drives the motor.
'''
import machine
import time

motorControlPin = machine.PWM(machine.Pin(15)) # Setup GP15 as the pin controlling the transistor with a PWM output
motorControlPin.freq(1000) # Sets the frequency of the pin
while True: 
    # First ramp up the output value - this speeds up the motor
    for outputValue in range(0, 65536, 100):
        motorControlPin.duty_u16(outputValue)
        time.sleep_ms(5)
        
    # Pause at full speed for 1 second to make it more obvious we have got here
    time.sleep(1)
    # Now ramp down the output value - this slows the motor down
    for outputValue in range(65536, 0, -100):
        motorControlPin.duty_u16(outputValue)
        time.sleep_ms(5)
    
    # Pause at stop for 1 second to make it more obvious we have got here
    time.sleep(1)

Inventors Kit Extra Resources:

Each of the ten experiments has been designed to ease you into coding and physical computing for the for the Raspberry Pi Pico. The experiments have been chosen to cover the key concepts of physical computing and they also increase in difficulty as you progress. This resource has been put together to provide additional information for this experiment and has not been designed to replace the booklet. The majority of the information you will need to perform and understand this experiment is contained in the booklet.

If you are new to coding and physical computing, even the least complex examples can be quite challenging. With this in mind, we created walk-through videos for each of the experiments. Our presenter talks you through the circuit in a way that backs up the information given in the booklet but in a style that some might find easier to absorb. As well as having a video walk-through, each page also contains the code. Although it is always good to tackle the code yourself, it can be handy for testing your circuit quickly. The code has been heavily commented as an extra learning resource. To get the most out of the experiment, once you've tested your circuit have a go at coding the experiment from scratch.

Follow the links in the table below:

Exp No. Experiment Name.
1
Digital Inputs & Outputs.
2
Light Sensor & Analog Inputs.
3
Dimming an LED using a potentiometer.
4
Using a transistor to drive a motor.
5
Control a servo with a potentiometer.
6
Setting the tone with a piezo buzzer.
7
Using a seven segment display.
8
Exploring wind power.
9
Capacitor charge circuit.
10
Controlling ZIP LEDs.

Leave a comment

All comments are moderated before being published