Calibrating the Light Sensor

7 - Determining Bloodhound’s Speed
using an Optical Transducer
Aim
Software
Resources Required
Difficulty
Understand some of
the programming
issues that arise
when using sensors.
Scratch
1.4
• Sparkfun PicoBoard
• Scratch software (version 1.4)
• Cardboard, Blue Tack and glue
• A cocktail stick
Previous Scratch
experience
required.
“In this module you will make a simple rotating disk. You will then measure the speed of this
disk by writing a Scratch program with the aid of a PicoBoard and its light sensor.”
How Fast?
If we know how fast the wheel is rotating, we can work out the car’s speed.
We can do this by putting a sensor on the wheel and timing how long each revolution takes. If
we know the diameter of the wheel, we can calculate and display the speed of the car.
How can we measure the rotational speed of a wheel?
- Use a Light Dependent Resistor. This is a resistor whose resistance
decreases as light intensity increases.
- Paint a light and dark patch on the wheel. As the wheel rotates, the
Light dependent
resistor, LDR
amount of light reaching the LDR will change with a consequent
change in the value of its resistance.
- In this module we will measure the light reaching an LDR with a spoked wheel.
- We will use a PicoBoard to read the sensor, and Scratch to calculate the speed of the wheel.
The above shows a cardboard wheel balanced on a cocktail stick, rotating over the LDR
on a PicoBoard. This is used to simulate a rotating wheel. The cocktail stick is shown here
mounted on a block of wood, but Blue Tack could also be used.
Produced at Warwick University by
Margaret Low ([email protected]),
Simon Leigh ([email protected]),
Bob Hodge
Funded by Bloodhound SSC
http://www.bloodhoundssc.com/
2
The Sensor Program
As described below, the sensor program has to:
• Count the number of disk rotations.
• From this, calculate the speed of the car.
• Display the results.
The program could be written as one large piece of code, but it would be easier to understand if
it was broken down into smaller modules. To achieve this we use three broadcast blocks or, as
they’re usually known in programming, subroutines. Later we will introduce a forth to calibrate
the PicoBoard’s light sensor.
The sensor works because the rotating disk interrupts light falling on the LDR. As the disk
rotates the output changes from ‘dark’ to ‘light’. We need to know what numerical value
corresponds to these two conditions, the magnitude of which will change according to the
level of ambient light. We will introduce a method of automatically achieving this later,
but for now we will set variables ‘Light zero’ and ‘Light one’ to fixed values. These can be
manually altered to suit the ambient light conditions.
The software counts the number of
disc rotations in a set period of time.
The speed needs to be
displayed in a form that
can be readily
interpreted by the driver.
Produced at Warwick University by
Margaret Low ([email protected]),
Simon Leigh ([email protected]),
Bob Hodge
If, for example, 10 rotations are detected in one second, the disk
is obviously rotating at 600 rpm (10 x 60).
Given the ratio between revolutions of the sensing disk and
Bloodhound’s wheel, and the circumference of the wheel, we
can calculate the speed of the car.
Funded by Bloodhound SSC
http://www.bloodhoundssc.com/
3
Count the Number of Rotations
in a Period
This broadcast procedure counts the number of rotations in a given period.
High
No
Looking for
High or Low?
Sensor >
Bright?
Low
Sensor <
Dark?
Yes
Set flag to look for Low
No
Yes
Set flag to look for High
Increment Period Count
Note that variables and the timer will have to be initialised at the start of the procedure.
The length of the timer period is something of a compromise:
• Too short and it may indicate that the sensor has stopped, whereas it may be moving slowly.
• Too long and the display will be slow to respond to rapid changes in speed.
Produced at Warwick University by
Margaret Low ([email protected]),
Simon Leigh ([email protected]),
Bob Hodge
Funded by Bloodhound SSC
http://www.bloodhoundssc.com/
4
Interpret the Results
Assume that one rotation of the sensor corresponds to 1 rotation of Bloodhound’s wheel.
Assume the circumference of Bloodhound’s wheel is 5m. If we count 100 rotations of the
sensor in 1 second, the speed of the car can be calculated as follows.
Speed
= 500m/s
(5 x 100)
= 30000m/min
(500 x 60)
= 1800000m/hr
(30000 x 60)
= 1800km/hr
(1800000/1000)
= 1116mph
(1800 *.62) [ 1km = 0.62 of a mile]
This calculation can be carried out using the Operator functions in Scratch, i.e.
Display the Results
The easiest way to display the results is simply to get the Sprite to say them:
However reading a numerical value takes time, when a graphical display gives a quick
visual indication of the car’s speed. An easy way to do this is to get the Sprite to draw
a line. Use the motion blocks to send the Sprite to the start of the line, i.e.
Calculate a value of a variable Y that corresponds to the speed. Put the
pen down and move to this new position.
Produced at Warwick University by
Margaret Low ([email protected]),
Simon Leigh ([email protected]),
Bob Hodge
Funded by Bloodhound SSC
http://www.bloodhoundssc.com/
5
Example Program
Produced at Warwick University by
Margaret Low ([email protected]),
Simon Leigh ([email protected]),
Bob Hodge
Funded by Bloodhound SSC
http://www.bloodhoundssc.com/
6
Calibrating the Light Sensor
The output of the light sensor is obviously affected by the amount of light in the room. In a bright
room its output when covered by a disk vane may actually be higher than when uncovered in a
dark room. Until now we have set the values ‘Light zero’ and ‘Light one’ manually, and adjusted
them as required. Although this works, an automated process would be better.
The output of the sensor underneath a rotating disk will look something like the following.
Mean
The calibration process involves rotating the disk, monitoring the sensor output and deciding
what sensor value corresponds to the sensor when uncovered and then covered.
We need to decide how to recognise a rotation of the disk. We could:
• Wait until the sensor reads ‘Dark’ followed by ‘Bright’, but the ambient light may have
changed slightly and the sensor may never quite reach ‘Dark’ or ‘Bright’.
• Wait until the sensor reading goes from less than ‘Mean’, to more than ‘Mean’ and then back
to less than ‘Mean’ again. The problem with this is that we could get a false result if the
sensor is turning slowly and two readings are therefore close to the mean value.
The best method is to define values for ‘Light zero’ and ‘Light one’ as shown below.
Mean
We can now define a rotation when the reading goes from less than ‘Light zero’ to more than
‘Light one’ and back to less than ‘Light zero’ again. The values of ‘Light one’ and ‘Light zero’ can
then be calculated empirically.
This is the main part of the code, which will need to be repeated for a couple of
seconds to ensure that the highest and lowest values have actually been detected.
Read the light sensor.
If the value is less than seen previously, store
it as a new ‘Dark’ value.
If the value is greater than seen previously,
store it as a new ‘Bright’ value.
Produced at Warwick University by
Margaret Low ([email protected]),
Simon Leigh ([email protected]),
Bob Hodge
Funded by Bloodhound SSC
http://www.bloodhoundssc.com/
7
Example program with Light Sensor Calibration
Produced at Warwick University by
Margaret Low ([email protected]),
Simon Leigh ([email protected]),
Bob Hodge
Funded by Bloodhound SSC
http://www.bloodhoundssc.com/
8
Possible Problems
• The system is very sensitive to the degree of ambient light. The subroutine to calibrate
the light sensor is an attempt to overcome this, but it is not always successful. The
program works best if there is a sharp contrast between light and dark. This would be
achieved if, for example, a torch was shining directly above the sensor.
• A better contrast between light and dark is achieved with the disc as close to the sensor
as possible.
• Scratch can be slow to respond to events on the PicoBoard, and this places an upper limit
on the speed that the system can detect. It may help if Scratch is run in turbo mode:
Sources of Help
General Scratch help
http://scratch.mit.edu/help/
The Scratch website includes many examples of Scratch
programs and tutorials.
PicoBoard setup
http://www.picocricket.com/picoboardsetupUSB.html
References
Sparkfun PicoBoard
https://www.sparkfun.com/products/10311
Acknowledgements
PicoBoard image
http://www.hobbytronics.co.uk/picoboard-scratch
Software
To download Scratch version
1.4
Produced at Warwick University by
Margaret Low ([email protected]),
Simon Leigh ([email protected]),
Bob Hodge
http://scratch.mit.edu/scratch_1.4
Funded by Bloodhound SSC
http://www.bloodhoundssc.com/
8
Template for Rotating Disc
Use this template, which is approximately to scale when printed on A4, to create the
rotating disc out of cardboard.
Rotating Disc
15cm
Pivot
Fold the pivot into a U shape and glue the ends under the disk, as show in the pictures on Page 1.
The disk should then balance on a cocktail stick supported in Blue Tack or a block of wood.
Produced at Warwick University by
Margaret Low ([email protected]),
Simon Leigh ([email protected]),
Bob Hodge
Funded by Bloodhound SSC
http://www.bloodhoundssc.com/