This project introduces some more advanced maths and science to a STEM project so it should be considered for pupils aged 14+.
A push switch or an on off switch can only have two possible states (or values), ON (or 1) and OFF (or 0).
An analogue input can have a continuous range of values such as, temperature, speed, pressure, loudness, pH, light level, etc..
The values are measured through the use of a transducer.
The transducer will input a range of values to the system as its resistance changes.
Potential divider sensor circuit
When using sensors such as an LDR, that function by changing their resistance, they must be arranged as part of a potential divider circuit.
A fixed resistor, R1 is connected between the 3V pin and the Sensor. The resistance of R1 needs to be similar to the range of resistances produced by the sensor under normal operating conditions, measured in Ohms, using a multimeter.
The Sensor is connected between R1 and the GND pin.
A wire is connected between R1 and the Sensor. This will provide an input voltage to the pin it is connected to on the micro:bit, pin 0, pin1 or pin 2.
Potential divider theory explained
A potential divider circuit is used when connecting a transducer to a computer control system to make sure the output from the transducer can be easily read by the computer.
In simple terms:
A transducer (like a sensor) usually gives a voltage that depends on the physical quantity it measures (like temperature or pressure).
The computer control system typically needs a specific range of voltages to understand the sensor's output.
A potential divider circuit helps adjust or "scale" the voltage from the sensor so that it falls within the range the computer can read accurately.
It does this by dividing the voltage in a controlled way using resistors, so the system gets a usable signal that corresponds to the measurement.
In short, the potential divider makes the sensor's output match the needs of the computer system
This project in produces the Raspberry Pi Pico.
The Pico is a small microcontroller for use to control electronics. It is about 5cm long with a series of input output pins along each side to connect it to electronic components.
It doesn't run like a full computer (no screen, keyboard, or mouse).
Instead, you write code on a regular computer, then send it to the Pico to make it do things.
It uses a special chip made by Raspberry Pi called the RP2040.
It is programmed in MicroPython using a suitable IDE such as Thonny.
Breadboard
Breadboard is a reusable plastic board with lots of tiny holes. It's used to quickly and easily build and test electronic circuits without needing to solder (permanently connect) anything.
The holes in a breadboard are connected in a special pattern that lets electricity flow between components (like wires, LEDs, resistors, and sensors).
You plug components and wires into the holes to connect them together.
If something doesn’t work, you can move things around easily to fix it.
Makes it easier to plan and test designs for circuits.
You can build circuits without soldering.
Watch the video and then connect an LED for output to the Pico as shown.
Code the Pico using Thonny to test the LED output (watc the vidio again for help with this).
Now to put it all together:
Create a while True: loop in.
Take a reading from the LDR.
By experimentation, find the default value you want to use to swith the lights on.
Create a conditional loop which will switch on the LED when then input falls below your default value.
Algorithm
Repeat forever:
Take input from the PIR sensor
If the input detects movement:
turn on a red light flashing
turn on an alarm bleeping
Else:
keep light and alarm off
PIR (passive infra red) sensors are commonly used in burglar alarms and office lighting systems to detect movement.
Connect the sensor’s power input (which may be labelled 'VCC' or '+3v') to the vcc pin of the PIR sensor to the VBUS (40)
Then the sensor’s GND (ground or Earth pin) to a GND pin of Pico (28)
Connect the output pin of the PIR sensor to gp28 (34).
Every second, the sensor Pico uses a loop to send a radio message to the alarm. It uses selection to send the message ‘movement’ if movement has been detected, or if no movement is detected it sends the message ‘still’.
You may need to adjust the sensitivity and timing of the sensor using a small screwdriver. Check the documentation that came with your sensor for details.
Pico circuit connections
First place the Pico on the breadboard.
Connect the ground pin of the PIR sensor to a GND pin of Pico (28).
Connect the output pin of the PIR sensor to gp28 (34).
Connect the positive vcc pin of the PIR sensor to the VBUS (40).
Next connect the positive leg of an LED (330 ohm resistor protected) to gp15 (20).
Connect the negative leg of the LED to GND (18).
Then we will place a buzzer on the breadboard and connect the positive terminal of the buzzer to gp16 (21).
Connect the the negative terminal to GND (23).
The circuit is ready so open Thonny and enter the code.
Thonny code
# import the pin function from the machine library
# import a utime library which handles time.
from machine import Pin
import utime
# then set up three pins
# one pin for the pir sensor INput
pir_sensor=Pin(28,Pin.IN)
# second pin for the led OUTput
led=Pin(15,Pin.OUT)
#the third for buzzer OUTput
buzzer=Pin(16,Pin.OUT)
# create the main program forever loop
while True:
# tab indent
#set a condition so that if the pir sensor detects movement, that is, if the level of gp 28 rises to 1
if pir_sensor.value()==1:
# tab indent
# then create a repeat 50 times loop
# for i in range 50
# tab indent
# toggle the led and the buzzer with 0.1 seconds in between.
for i in range(50):
led.toggle()
buzzer.toggle()
utime.sleep(0.1)
# If the pir sensor does not detect any movement
# then keep the led off and the buzzer off as well.
else:
led.value(0)
buzzer.value(0)
Now let's save the file and run the code.
Sensitivity is useful for adjusting the range of motion detection:
If you want the sensor to detect motion from further away, you would increase the sensitivity.
If you only want it to detect close-range motion (to prevent the sensor from detecting people or animals far in the distance), you would lower the sensitivity.
Hold Time is useful for determining how long the sensor remains active:
If you want the sensor to keep indicating motion for a while after detection (perhaps for a security system or an automatic light that stays on after detecting motion), you would set a longer hold time.
If you prefer quicker resets (so the sensor only stays active briefly), you would shorten the hold time.
By adjusting these two settings, you can fine-tune the PIR sensor’s behavior to match your specific needs, whether it’s for security, automation, or other applications.
To learn more about the ultrasound proximity sensor, review Stage 2 when it was used with the micro:bit.
In this project, the sensor will be used with the Pico to measure distance.
Python code to measure distance using the pico
from machine import Pin
import utime
trigger = Pin(13, Pin.OUT)
echo = Pin(14, Pin.IN)
def ultra():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
distance = round(distance, 0)
print("The distance from object is ",distance,"cm")
return distance
while True:
distance = ultra()
utime.sleep(1)
10K NTC thermistor
(temperature sensor)
Thermistor
A thermistor is used to sense temperture changes.
Certain electronic components create some resistance to the flow of electric current passing through them.
The resistance of a thermistor changes as its temperature changes.
The higher the temperature, the lower the resistance, so the higher the input voltage to the Pico.
In the MicroPython code we need to do the following steps:
We measure Vout using the ADC on Raspberry Pi Pico
We calculate Rt using the Voltagee Divider Equation
Then we use Steinhart-Hart equation for finding the Temperature. It is not necessary to understand the physics of thermistors to be able to use the maths in your code.
Finally we convert from degrees Kelvin to Celsius
Thonny code
from machine import ADC
from time import sleep
import math
adcpin = 26 # analogue to digital converter pin
thermistor = ADC(adcpin)
# Voltage Divider
Vin = 3.3
Ro = 10000 # 10k fixed resistor - adjust this figure if a different value resistor is used.
# Steinhart Constants
A = 0.001129148
B = 0.000234125
C = 0.0000000876741
while True:
# Get Voltage value from ADC
adc = thermistor.read_u16()
Vout = (3.3/65535)*adc
# Calculate Resistance
Rt = (Vout * Ro) / (Vin - Vout)
# Rt = 10000 # Used for Testing. Setting Rt=10k should give TempC=25
# Steinhart - Hart Equation to calibrate temperature from a non-linear relationship
TempK = 1 / (A + (B * math.log(Rt)) + C * math.pow(math.log(Rt), 3))
# Convert from Kelvin to Celsius
TempC = TempK - 273.15
# Print the Celsius temperature to one place of decimals
print(round(TempC, 1))
sleep(5)
Now that we have a system to measure temperature, this can be used to control any number of temperature dependent applications such as heating systems, cooling systems, high temperature alarm systems, low temperature alarm systems, etc.
Cooling fan control
We need to code a transduser input to tell us when the temperature is above the default value of say 23 degrees Celsius.
The default is the maximum desirable fixed temperature that we do not want our room to go above.
Algorithm
Repeat forever:
Measure the temperature
If the temperature goes above the deffault value:
fan turns on
If the temperature falls below the diffault value:
fan turns off
from machine import Pin # This imports the 'pin' library from machine.
fan = Pin(25, Pin.OUT) # The fan is connected to pin 25 for output.
fan.value (1) # Will turn the fan on
fan.value (0) # Will turn the fan off
Now you must code a conditional if else statement within the while True: loop of the code to measure temperature shown above.
This must turn the fan on when the temperature value is greater than 23 degrees Celsius.
if round(TempC, 1) > 23:
else:
You will also need some circuit components to make and test the circuits.
You use electrical switches many times every day.
These switches will be mechanical devices that operate by making or breaking a circuit.
By using 'switches' (in fact they are transistors) computers are able to make the decisions needed to process data.
A simple switch can have two states, either ON or OFF.
There are a number of different terms used to describe when a switch is on or off (see the table on the right).
One way of thinking of a switch is as a human interface with an electrical circuit.
It is the way in which we input our wish for a circuit to be on or off.
1. Two-key bank vault circuit
For greater security, a bank vault can only be opened if the bank manager AND the deputy manager are both present.
They each have a different key which operates one of two switches.
Both switches must be closed to open the vault.
This is an example of an AND gate.
Make the circuit shown and test it.
Why is this circuit called an AND gate?
2. Automatic shop door circuit
An automatic shop door is operated by two sensors.
There is a sensor facing the pavement outside the shop and another facing into the shop.
If the outside sensor detects movement it opens the door.
If the inside sensor detects movement it opens the door.
If BOTH sensors detect movement the door will open.
This is an example of an OR gate.
Make the circuit shown and test it.
Why is this circuit called an OR gate?
Truth tables help us to understand the behaviour of logic gates and how they are able to make the decisions needed to process data.
They show how the input(s) to a logic gate relate to its output(s).
All of the possible input combinations are written in the left hand columns of the truth table, A, B, C etc.
The gate's outputs are written in the right hand column, Out.
The NOT gate truth table and circuit symbol
The simplest logic gate is the NOT gate.
It is used in combination with other gates to invert the input.
So if the input A to the NOT gate is ON or 1, then the output will be OFF or 0.
If the input is OFF or 0, then the output will be ON or 1 (see diagram below).