LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)
Home > Sensors with the micro:bit and MakeCode

Sensors with the micro:bit and MakeCode

What it’s About

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:

  • Pushbutton contact, motion detection, tilt detection, directional pushbutton contact
  • Rotational position, moisture level, liquid level,
  • distance to an object
  • Black/white stripes, pressure on a surface
  • Presence or absence of a nearby object

Before you start

You will need:

  • Sensor Pack for the micro:bit (32735)
  • micro:bit v2 GO Bundle (32717)
  • USB A to Micro B cable
  • Computer with at least one USB A port
    (or a USB port with a USB C to A adapter)
  • An up-to-date Chrome or Microsoft Edge browser.

Complete these chapters first:

  • Get started with micro:bit and MakeCode
  • Writing micro:bit programs

After you finish

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:

  • cyber:bot – robotic responses to sensor measurements

Build your Prototyping Setup

Your prototyping setup will resemble this when you are done with this activity.

Carefully follow the instructions in these activities:

  • Connect the micro:bit to a Breadboard
  • What is a Breadboard?
  • Parts & Circuit
  • How to Connect Two Wires
  • Set Power for Circuits

Circuit

  • Make sure your micro:bit is still running continuity_tester.hex from First Electrical Connections with a Breadboard.
  • Unplug the USB cable.
  • Connect the black and red jumper wires exactly as shown here:

Reminder

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:

  • Get started with micro:bit and MakeCode
  • Writing micro:bit programs

Pushbutton

High/Low Signal Output

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.

Single Line

Pushbutton

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. 

Parts

(1) Tact Switch 400-00002
(1) Resistor 220 Ω (red-red-brown-gold)
(1) Resistor 10 kΩ (brown-black-red-gold)
(1) Jumper wire (red)

Schematic

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.

Wiring

Script

New to writing micro:bit programs with MakeCode?  Here are two links that will help you get started.

  • Get started with micro:bit and MakeCode
  • Writing micro:bit programs

After learning to write scripts and load them into the micro:bit, build and run this program.


Tests

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.

PIR Motion

PIR Motion 

Parts

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

Schematic

Wiring

Script

Tests

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.

4-Direction Tilt

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.

Parts

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

Schematic

Wiring

Script

Tests

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.

How It Works

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.

5-Position Switch

5-position switch 

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.  

Parts

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

Schematic

Wiring

Script

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.

Tests

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:

  • U: up
  • R: right
  • D: down
  • L: left
  • C: center, into toward the breadboard

Potentiometer Rotation

Potentiometer 

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.

Parts

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

Schematic

Wiring

Script

Tests

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.

Moisture Sensor Probe

Moisture Sensor Probe 

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.

Parts

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

Schematic

Wiring

Connect the harness to the probe with:

  • Red to VCC
  • Black to GND
  • Yellow to AOUT

Then, connect red wire to 3.3 V, the black wire to GND and the yellow wire to P2 through a 220 Ω resistor

Script


Tests

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.   

Mini Liquid Level Sensor

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.

Parts

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

Schematic

Wiring

Connect the harness to the probe with

  • Red to VCC
  • Black to GND
  • Yellow to AOUT

Then, connect red wire to 3.3 V, the black wire to GND and the yellow wire to P2 through a 220 Ω resistor.

Script


Tests

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.   

Phototransistor Light Sensor

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.

Parts

(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

Schematic

Wiring

Script


Tests

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.  

2-Axis Joystick

2-Axis Joystick 

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.

Parts

(1) 2-Axis Joystick 27800 
(2) Resistor 220 Ω (red-red-brown-gold)
Jumper wires: (2) Red, (1) black

Schematic

Wiring

Script – Vertical and Horizontal Voltages


Tests

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.

Script – Joystick Matrix Pixel Control


Tests

Verify that the “pixel” on the micro:bit module’s 5×5 LED matrix display lights up and indicates the position of the joystick.

LaserPING Distance Sensor

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.

Parts

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

Schematic

Wiring

Script


Tests

Face the parts side of the laser ping at various target objects and compare their ruler measured distance to the micro:bit measured distance.

Phototransistor Light (Extended Range)

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.

Parts

(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

Schematic

Use the 0.01 µF capacitor for the C1 value.  If lighting is too bright, switch to the 0.1 µF capacitor.
Wiring.

Wiring

Script


Tests

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.

QTI Line and Close-range Object Detection

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.

Parts

(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

Schematic

Wiring

Script


Tests

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.

FlexiForce Pressure Sensor

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

Parts

(1) FlexiForce Sensor Demo Kit 30056 
(1) 0.01 µF capacitor (103)
(1) 220 Ω resistor (red-red-brown-gold)

Schematic

Wiring

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.

Script


Tests

Lightly pinch the gray circle at the end of the FlexiForce sensor.  As you increase pressure, the ms time will decrease. 

Infrared Object DetectionCopy

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.

Parts

(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

Schematic

Wiring

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

Assemble the IR Headlight

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.

  • Insert the infrared LED into the LED standoff tube, threading the anode and cathode leads through their respective holes in the bottom.  If properly aligned, the LED should drop in place without pushing.
  • Slip the short tube over the IR LED’s clear plastic case, tab end first. The tabs should snap into the standoff, holding the LED in place.

Breadboard Connections

Script


Tests

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.

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024


Source URL:https://learn.parallax.com/courses/sensors-with-the-microbit-makecode/
Links