In this series of activities, you will connect sensors to the micro:bit and measure them with simple scripts. Here are some of the things you will measure:

You will need:
Complete these chapters first:
You will be able to build and write scripts for a variety of sensor circuits. For more micro:bit applications that include sensors, indicators, and actuators, try these series:
Your prototyping setup will resemble this when you are done with this activity.

Carefully follow the instructions in these activities:

Make sure you know the basics of writing micro:bit programs and how to load them into the micro:bit by completing these chapters first:
Our first class of sensors have high low signal outputs. These are very easy to read with a micro:bit script. We’ll start with sensors that send high/low signals that indicate detection on a single line. Then, we’ll expand to sensors that report with high/low signals on multiple lines.
The pushbutton is also called a tact switch. When not pressed, a pushbutton like the one below leaves the closer pair of pins disconnected from the ones in the back. When pressed, it shorts the front row to the back row.

(1) Tact Switch 400-00002
(1) Resistor 220 Ω (red-red-brown-gold)
(1) Resistor 10 kΩ (brown-black-red-gold)
(1) Jumper wire (red)
This circuit allows the pushbutton to apply a high (3.3 V) signal to the P8 I/O pin when pressed, and a low (0 V) signal when not pressed.


New to writing micro:bit programs with MakeCode? Here are two links that will help you get started.
After learning to write scripts and load them into the micro:bit, build and run this program.
Verify that the micro:bit’s 5×5 LED matrix display shows the checkmark ✔ for 1 second after each time the button is pressed. It should otherwise display an X.

(1) PIR Sensor with LED Signal 555-28027
(1) Resistor – 220 Ω
Jumper wires: red, black


Verify that the micro:bit’s 5×5 LED matrix display shows the checkmark ✔ for around five seconds after motion is detected. With no motion, it should display an X.
The 4-directional tilt sensor reports tilt if it is tilted by more than roughly 30°. It reports this tilt with high/low signals it sends with its OUT1 and OUT2 pins.

(1) 4-Directional Tilt Sensor 28036
(2) 220 Ω resistors
Jumper wires: blue, red, black


Tilt the sensor in 4 directions and see the four letters: A, B, C, and D. Note, if the sensor is lying flat, it will display position C.
Inside the sensor, there is a light, a ball, and two light sensors. Imagine rotating the sensor like a steering wheel. When A is up and C is down, the ball covers the light, so both sensors transmit 0 V. If B is up, phototransistor 1 is covered and OUT1 is 0 V. But, phototransistor 2 detects light, so OUT2 is 3.3 V. In position C, the ball is in the far corner, so both phototransistors detect light and both OUT1 and OUT2 send 3.3 V. In position D, the ball covers the other phototransistor, so OUT1 is 0 V and OUT2 is 3.3 V.

This is like a thumbstick or joystick, but without levels. While a thumbstick can tell how far you have pushed in some direction, the 5-position switch only knows if it has been pushed up, down, left, right, or not at all.

(1) 5-Position Switch 27801
(4) Jumper wires
(5) Resistors – 220 Ω (red-red-brown-gold)


The 5-position switch has built-in “external” pull-up resistors. So all built-in pull-up/down resistors in the micro:bit I/O pins used in the script are disabled with …NO_PULL settings.
Without any pushing on the button lever, the 5-position switch will be in its detent position and micro:bit module’s 5×5 LED matrix display will display a small square. If you press up, right, down, left, or center (into toward the breadboard), the micro:bit will display the first letter of the direction:
The potentiometer is a resistive element between two terminals with a middle “wiper” terminal that contacts that element somewhere along its length. The contact point is adjusted by turning the adjustment knob on top of the device. With its end terminals connected to a voltage supply, the wiper terminal voltage indicates how fare between the terminals the knob has been turned.

(1) Potentiometer 152-01031
(1) 220 Ω resistor (red-red-brown-gold)
Jumper wires: red, black


As you twist the potentiometer from one end of its range of motion to the other, the measurements displayed by the micro:bit module’s 5×5 LED matrix display should range from 0 to 3.3 V.
The moisture sensor applies voltage to the two plated electrodes. In more moist soil, the plated electrodes conduct more electric current. In less moist soil, the electrodes conduct less electric current. The transistor circuit in the sensor represents the higher and lower currents with higher and lower voltages, transmitted by the AOUT (analog out) pin.

(1) Moisture Sensor Probe 28092 includes probe and harness
(1) Resistor 220 Ω (red-red-brown-gold)
Jumper wires: Red, black

Connect the harness to the probe with:
Then, connect red wire to 3.3 V, the black wire to GND and the yellow wire to P2 through a 220 Ω resistor

Insert the probe into soil so that it covers the plated metal prongs. Then, monitor micro:bit module’s 5×5 LED voltage display as you add water, then let it disperse and then evaporate over time. The higher the voltage, the higher the moisture content.

The Mini Liquid Level Sensor is similar to the moisture probe in that it applies voltage and detects current through the liquid. As with soil, the current flow is made possible thanks to minerals in the water, which make it possible to for electrons to move between probes (electric current). The sensor converts this current to voltage that can be measured by the micro:bit. As the level increases, so does the voltage. Tap water is recommended because distilled or fully purified (deionized) water does not contain the minerals needed to conduct electricity.

(1) Mini Liquid Level Sensor (28090) includes probe and harness
(1) Resistor 220 Ω (red-red-brown-gold)
Jumper wires: Red, black

Connect the harness to the probe with
Then, connect red wire to 3.3 V, the black wire to GND and the yellow wire to P2 through a 220 Ω resistor.
Liquid level can be below the sensor (0 V). As it covers more of the plated lines, the voltage measurement will increase. Monitor the micro:bit module’s 5×5 LED voltage display as you add water. IMPORTANT: Do not allow the water level to go above the plated lines.

A phototransistor allows more current through a circuit when as more light strikes the light collecting surface under its clear dome top. One type of sensing circuit is a supply voltage with the phototransistor connected in series with a resistor. The voltage between the resistor and the phototransistor is proportional to the current the phototransistor allows through. With more light, the voltage increases, and with less light, the voltage decreases.

(1) Phototransistor 350-00029
(1) 220 Ω resistor (red-red-brown-gold)
(1) 10 kΩ resistor (brown-black-orange-gold)
(1) 2 kΩ resistor (red-black-red-gold)
Jumper wire: Red


Direct sunlight is too bright for this sensor. Close the blinds. Check the voltage measurement on the micro:bit module’s 5×5 LED matrix display without any shade. Then, gradually increase the level of shade and monitor the voltage as it drops. In rooms with brighter light, replace the 10 kΩ resistor with the 2 kΩ resistor.
The 2-Axis Joystick’s position is measured by two potentiometers, one for the forward and backward motion and the other for left and right.
(1) 2-Axis Joystick 27800
(2) Resistor 220 Ω (red-red-brown-gold)
Jumper wires: (2) Red, (1) black


Use the previous program at the beginning of this section to measure the sensor’s voltage output.
After loading the script into the micro:bit, click Show serial in the micro:bit Python Editor. There, you will see the voltage measurements updated every second.
Verify that the “pixel” on the micro:bit module’s 5×5 LED matrix display lights up and indicates the position of the joystick.
The LaserPING sensor measures the time it takes infrared light to make a round trip from the laser ping, to an object, and back and sends it to the microcontroller as a pulse (a brief high signal). The amount of time the signal stays high indicates the time of flight.

(1) LaserPING 28041
(1) Resistor 220 Ω (red-red-brown-gold)
Jumper wires: Red, black


Face the parts side of the laser ping at various target objects and compare their ruler measured distance to the micro:bit measured distance.
This is the same phototransistor circuit that was used earlier. This one can measure over a wider range of light levels, which might be more convenient than swapping out resistors to get a good response in a different ambient light situation.

(1) 850NM T1 3/4 Phototransistor (350-00029)
(1) Resistor 220 Ω (red-red-brown-gold)
(1) 0.01 µF capacitor (103)
(1) 0.1 µF capacitor (104)
Jumper wire: Black
Use the 0.01 µF capacitor for the C1 value. If lighting is too bright, switch to the 0.1 µF capacitor.
Wiring.


Unlike the phototransistor circuit with a resistor in it, this version displays higher numbers when the light level is lower. For brighter lighting, replace the 0.01 µF capacitor with the 0.01 µF capacitor. Verify that the darker the shade, the higher the number.
The QTI is most often used as a robotic line sensor. It shines infrared light, and if a surface within about 1/4-inch reflects that light the script will respond with a smaller value. If the surface is black, it will absorb the light and respond with a larger value.

(1) QTI module 555-27401
(1) 3-Pin Signal-Power-Ground Cable Extension (8″) 800-00080 (Optional)
(1) 3-Pin Single Row Header (long) (451-00303) (Optional)
(1) Resistor 220 Ω (red-red-brown-gold)
Jumper wires: Red, black


The QTI is designed to face downwards close to either a black or white surface. Make sure it is not facing a window with sun streaming in or any other bright source of light.
Use a sharpie to create a black 1×1 inch black square in the corner of a sheet of paper.
Face that 1×1 inch black square 1/4 of an inch from the parts side of the QTI and note the measurement.
Now, face a corner the surface of a corner of the same sheet that white, again at a distance of 1/4 inch.
The microsecond time measurement should be less than 1/4 of the previous measurement.
The FlexiForce sensor decreases resistance as pressure is applied its gray circle. Just pinch the gray circle with your fingertips against the flat faces (not the edges).
(1) FlexiForce Sensor Demo Kit 30056
(1) 0.01 µF capacitor (103)
(1) 220 Ω resistor (red-red-brown-gold)

Important: Due to the camera angle, the photo might make it look like the resistor is plugged into the breadboard row next to P7. It is really plugged into the breadboard row next to P8.

Lightly pinch the gray circle at the end of the FlexiForce sensor. As you increase pressure, the ms time will decrease.
This infrared detection system is kind of like an infrared flashlight. If the invisible beam from the IR LED reflects off an object and is picked up by the IR receiver, it’ll send a low signal. Otherwise, it sends a high signal. One thing about the infrared beam, it has to be flickering on/off at 38 kHz (38,000 times per second) for the IR detector to “see” the reflection.
(1) LED shield 350-90005
(1) LED standoff 350-90004
(1) IR LED 350-00003
IR Receiver 350-00039
(1) 2 kΩ resistor (red-black-red-gold
(1) 220 Ω resistor (red-red-brown-gold)
Jumper wires: Red, (2) black

Make sure you are using an infrared LED and not phototransistors. The infrared LED has a taller and more rounded plastic dome:

The infrared LED is designed to sit in the standoff tubes a certain way. The flat spot on the LED dome matches a flat ledge inside the tube. Holes on the bottom of the tube are labeled for the anode (+) and cathode (-) leads.


Face an object like a box, your hand, or a piece of paper so that the infrared LED shines light at it. The distance should be 6 inches or less. The micro:bit should respond by displaying a checkmark. Take the object away, and the micro:bit should respond by displaying an X.