A push switch or an on off switch is a binary device. It can only have two possible states (or values) either ON (1) or OFF (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.
The values could range from 1 all the way up to 100 for example.
When using a sensor or transducer such as an LDR, that functions by a change in its resistance, must be arranged as part of a potential divider circuit.
This is a cheap and simple way to get analog voltage outputs which can be measure directly in real-time with simple electronics.
Study the example below which uses a thermistor as a sensor to measure temperature.
Potential divider sensor 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.
This can be measured in Ohms using a multimeter (see picture on the right).
The Sensor is connected between R1 and the GND pin.
A wire connected between R1 and the Sensor will provide a voltage to an input pin on the micro:bit. In this case it is connected to pin 2.
Potential divider theory explained
Do not try to explain this to children younger than Grade 9.
This is quite advanced electronics and they do not need to understand this to use an analogue sensor.
The Grade 9 teacher does not need to understand this principle either.
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
The analoge inputs from a sensor may range from a low of 0 to a maximum of 1023.
Typically, an LDR being used as a light meter might give a raw_data range of 500 with bright light to 1021 when dark.
A much more understandable and useful range of values would be 0 for dark to 100 for bright light.
Using the MAP function, it is possible to map the raw_data range onto a much more helpful 0 to 100 range.
So.....
The low raw_data value 500 from the brightest light is mapped to a reading of 100.
The highest raw_data value 1023 when dark is mapped to a reading of 0.
Calibrating script for a thermister thermometer
Button A is used to identify the range of analogue input raw_data values by experimentation.
Button B will map analogue input raw_data values to more sensible range of output readings.
Connect an LDR in a digital divider circuit and a moisture sensor in a digital divider circuit to pin 0 and pin 1 of a microbit.
Collect a range of analogue raw_data from both sensors to establish the working range.
Map the raw_data to give two sensible scales of readings, one for light and the other for moisture.
Code the system, download it to a micro:bit and test it.
It may need some further fefinement when tested in the field.
DIY built buggy with ultrasound sensor and two IR sensors
Kitronik Autonomous Robotics Platform for Pico
There are two options for a vehicle/buggy robotics platform. Either purchase the individual components and build your own DIY buggy or buy a commercial one with a built in Kitronik Pico robotics board.
The Kitronik robotics platform retails at about UK£41.
Advantages - all of the wiring has been done for you and the sensors can simply be plugged into the platform for easy connection to the Pico. When running, their performance is likely to be more reliable and more responsive.
Disadvantages - everything is done for you which limits the scope for customisation, experimantation and in depth learning. If the platform is damaged or develops a fault it may not be possible to repair it.
DIY buggy - assuming you are building the platform out of scrap materials, it will cost you approximately UK£24 (for the x4 AA battery holder, Pico, Kitronik Pico robotics board, x2 geared motors, x2 IR sensors, x1 PIR sensor) so a saving of about UK£17.
Advantages - building the DIY platform is a tremendous learning opportunity. The buggy can be customisted for a bespoke size and shape. The mechanics can be changed to meet the design specifications of a wide variety of different projects. For example, the two wheels can be replaced with tracks driven by four motors for offroad capability. The chassis and wheels can be replaced by the hull to make a remote controlled or autonomous boat. If the DIY platform is damaged it can easily be repaired or the parts recycled.
Disadvantages - you will need to learn how to wire different components to the robotics board. Or perhaps this is actually another advantage? You cannot use the platform straight out of the box. Faults in the wiring can lead to failure of the buggy, requiring carefull fault finding and correction. But then again, these are practical skills we want our pupils to develop.
When my pupils are working on robotics projects based on a vehicle of some description, I always get them to design and build their own platforms or I give them a basic chasis for them to build onto. I want them to gain a good working knowledge of the wiring needed to connect input and output components to the processor (Pico) and how this relates to the code they will have to write. The pedagogy of this approach is vastly superior to following a set of manufacturers instructions.
See Stage 5 - Measuring distance
For Pico robotics work using a Kitronik robotics board, set up your Thonny micropython script as follows:
import PicoRobotics
import utime
board = PicoRobotics.KitronikPicoRobotics()
from machine import Pin
import PicoRobotics
import utime
trigger = Pin(2, Pin.OUT) #The K robotics board has 26 IO pins. The trigger is connected to pin 2 on the Pico.
echo = Pin(4, Pin.IN) #The echo is connected to pin 4 on the Pico.
board = PicoRobotics.KitronikPicoRobotics()
def fwd():
board.motorOn(1, "f", 30)
board.motorOn(2, "f", 30)
def rt():
board.motorOn(1, "f", 30)
board.motorOn(2, "r", 30)
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()
if distance < 25:
rt()
else:
fwd()
utime.sleep(1)
You may have met the PIR sensor in Stage 6
In this project you will learn how to use a PIR sensor to control a self driving vehicle to avoid pedestrians.
For this project you can use the micro:bit or the Pico.
In this example we will use the micro:bit.
PIR (passive infra red) sensors are commonly used in burglar alarms and office lighting systems to detect movement. You can connect one to a micro:bit's pins (see diagram) to trigger an alarm when movement is detected.
Set the jumper to H (high): repeat trigger using the yellow jumper cap.
Connect the sensor’s power input (which may be labelled 'VCC' or '+3v') to the micro:bit’s 3v pin, then the sensor’s GND (ground or Earth pin) to GND on the micro:bit.
Next connect the signal output ('S') on the sensor to pin 3 on the micro:bit. The diagram shows the output connected to pin 0, but that will be used to control the motors so you will need to use pin 3 for this project.
Every second, the sensor micro:bit 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’.
The alarm micro:bit shows a stick-figure on its LED display output and sounds an audible alarm when there’s movement.
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.
The IR sensor mainly consists of an IR transmitter (IR LED) and an IR receiver (Usually a photodiode).
The IR LED always emits IR rays in the direction it is pointing .
Now let us bring it closer to a surface. When the IR rays hit a surface, some rays will be reflected back depending upon the color of the surface.
The brighter the color, the more IR will be reflected back.
The darker the color is, more IR will be absorbed by the surface and less IR will be reflected back.
These reflected rays are received by the photodiode and depending upon the intensity of the received IR rays, the resistance of the photodiode varies which will, in turn, vary the output voltage.
IR sensors, commonly used in line-following robots, detect the presence of a line by emitting infrared light and detecting the reflected light, allowing the robot to navigate based on the contrast between the line and the background.
Thus it is possible to sense the color of the surface where the robot is running by looking into the reflected IR rays. By measuring how bright the surface is, the robot can track a black line drawn onto the surface and to follow it.
There are so many cheap IR sensors available online. You will need at least two of them for making the line following robot.
Two IR sensors
The sensors are mounted on the front underside of the buggy, pointing down and nearly touching the ground surface. In the DIY buggy they are attched to a vertical bracket on the front of the buggy using rubber bands. This allows for some height adjustment.
import PicoRobotics
import machine
import utime
#Robot 6 - Setup
board = PicoRobotics.KitronikPicoRobotics()
from machine import Pin,PWM #importing required PIN and PWM from modules
right_ir = Pin (27, Pin.IN) #The K robotics board has 26 IO pins. The right IR sensor is connected to pin 34 on the Pico, coded 27 for input.
left_ir = Pin (28, Pin.IN) #The K robotics board has 26 IO pins. The left IR sensor is connected to pin 32 on the Pico, coded 28 for input.
# Move Forward
def move_forward():
board.motorOn(1, "f", 19)
board.motorOn(2, "f", 21)
#Turn Right
def turn_right():
board.motorOff(1)
board.motorOn(2,"f", 15) #if motor needs to be slower during turning
#Turn Left
def turn_left():
board.motorOff(2)
board.motorOn(1,"f", 15) #if motor needs to be slower during turning
#Stop
def stop():
board.motorOff(1)
board.motorOff(2)
while True:
right_val=right_ir.value() #Read Sensor 1 Value
left_val=left_ir.value() #Read Sensor 2 Value
print(str(right_val)+"--"+str(left_val))
#Set Conditions for line following
if right_val==0 and left_val==0:
move_forward()
elif right_val==1 and left_val==0:
turn_right()
elif right_val==0 and left_val==1:
turn_left()
else:
stop()
if __name__ == "__main__":
main()