This Experiment Requires:
- BBC micro:bit. x 1
- BC337 NPN Transistor. x 1
- 2.2KΩ Resistor. x 1
- Terminal Connector. x 1
- Low Inertia Solar DC Mini Motor. x 1
- 3 x M/F Jumper Wires (if using the Prototyping System).
- 1 x Fan Blade.
- 5 x Crocodile Leads (if not using the Prototyping System).
The Aims Of This Experiment Are:
- To use a transistor to drive a fan motor.
- To control the speed of the motor using Pulse Width Modulation (PWM).
Experiment 4 Video Walk Through:
Experiment 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 BBC micro:bit 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.
Creating The Code Using The Microsoft Block Editor:Below is an example of how to write the code for the experiment in the Microsoft Block Editor. If you're having difficulty producing your own code for this experiment, we've created the code for you that you can download directly from the embedded editor above..
Building This Circuit On The Prototyping System:It is possible to build this circuit using the Prototyping System for the BBC micro:bit and the components listed at the top of the page, you can follow the diagram below.
Building This Circuit With Crocodile Leads:If you don't have the Kitronik Inventors Kit for the BBC micro:bit or the Prototyping System you can still build this circuit using the circuit diagram for reference and Crocodile Leads to join the components together. You can see an example of this in the picture below. When connecting the Transistor it is important to correctly identify the Base, Collector and Emitter leads. These can be seen in the picture to the right. Care should also be taken when connecting the transistor to ensure that there are no shorts, you should bend the pins away from each other before you connect the crocodile leads.
What Will Happen:The motor should after a few seconds start to spin slowly, then faster until it reaches a maximum speed. At this point, will slow down and stop and the cycle will begin again.
What's Going On (How Does It work)?:If the transistor is turned on and off very quickly and it spends half its time on and half its time off then the fan motor will spin at half speed. By changing the percentage of the time that the transistor spends on (known as the duty cycle) the speed of the motor can be finely controlled. As the power pulses on and off and the width of the pulses are controlled, we call this process Pulse Width Modulation (PWM). The code works in two stages. The first loop writes the duty value to Pin P0 then increases the duty value by one and writes the value to P0 again. This repeats until the value reaches the maximum of 1023 (full speed). The second loop then kicks in and reduces the duty by 1 and writes it to P0 until the value reaches 0 (stopped). This whole cycle is inside a forever loop so the motor will speed up and slow down forever. The PWM output varies the duty cycle of the output voltage, as show below, to vary the speed of the fan motor.
Code Downloads:The code examples below have been individually zipped and can be downloaded by clicking on your preferred option. Once unzipped you can either open the and edit the code in appropriate editor or place the HEX file onto your microbit*.
MakeCode Editor & Python Code Downloads:
- This code was created with the MakeCode Editor, download the HEX file here.
- This code was created with the Python Editor, download the HEX file here.
Legacy Editors Code Downloads:NOTE: The legacy editors will disappear at some undisclosed point in the future. The code can still be placed on a microbit and will run regardless but you won't be able to use an editor. When that happens, switch to using one of the currently supported editors. We've already made the switch as the new editors are better and more fully featured.
- This code was created with the Blocks Editor, download the HEX file here.
- This code was created with the Touch Develop Editor, download the HEX file here.
Kitronik Inventors Kit Resources:
|Exp No#.||Experiment Name.||Resource Type.|
|1||Say Hello to the BBC micro:bit.||Further Help.|
|2 Pre V1.7||Using an LDR and analog inputs.||Full Experiment + Further Help.|
|2 V1.7||Using a Light Sensor & analog inputs.||Full Experiment + Further Help.|
|3||Dimming an LED using a potentiometer.||Further Help.|
|4||Using a transistor to drive a motor.||Full Experiment + Further Help.|
|5||Using the accelerometer to control motor speed.||Further Help.|
|6||Setting the tone with a piezo buzzer.||Further Help.|
|7||Wind Power.||Full Experiment + Further Help.|
|8||Making a game using the compass.||Further Help.|
|9||Capacitor charge circuit.||Further Help.|
|10||Using an RGB LED.||Further Help.|
|11||Making a pedestrian crossing.||Full Experiment + Further Help.|
|12||Making a random dice.||Full Experiment + Further Help.|
Get The Kitronik Inventors Kit For The BBC microbit:We do two versions of the Inventors Kit for the BBC micro:bit, with or without the BBC micro:bit included. Chose the option that is right for you from the links below. [product id="1696"] [product id="1740"]
©Kitronik Ltd – You may print this page & link to it, but must not copy the page or part thereof without Kitronik's prior written consent.