Bozoma Innovation Hub · Arduino Ladder · Band 4
Night Guard
Buttons only ever say yes or no. Sensors give you a number. In this band you learn to read those numbers, and to find out the hard way that a number from a sensor means nothing at all until you decide what it means.
About 7 hours. Finish Band 3 first. You will need the serial monitor constantly.
Choose your build
- CoreNight Guardthe light that switches itself on. This page. Everybody builds this one.
- PracticeThe dimmable lampA knob and a light, with three traps in it you can find with a pen before you find them with the board. One of them is Band 6 arriving two bands early.
- SwapThe rain alarmWashing on the line, and a number that goes down when it gets wet. Calibration you cannot avoid doing.
- SwapThe room monitorA sensor that hands you degrees and a percentage, already meant. The one time somebody else did the calibrating.
Swap means build it instead of the one on this page: it teaches the same things and leaves you just as ready for the next band. Everything else on this page still applies, including the Check yourself questions. Practice is short, comes after the core, and gives you no sketch: the job, the check, and the code is yours. Bench is not a build at all: it is an instrument or an idea, done once, and everything after it is easier for having done it.
Sketches for this band
- b4_01_pot_read_given.ino
- b4_02_pot_dim_led.ino
- b4_03_light_read.ino
- b4_04_buggy_intdiv.ino
- b4_05_night_guard_worked.ino
Click one to save it, or get every sketch at once from the downloads page. Each file opens with a plain-English header telling you what it does and how to wire it.
What this band is for
Everything the board has read so far has been on or off. A button is pressed or it is not. There is no in between.
The world is not like that. Light is not on or off. It is dim, or bright, or somewhere between. Temperature is not hot or cold. To build anything that reacts to the real world, the board needs to read amounts, not just yes and no.
That is what this band gives you, along with the most important lesson in the whole course about working with real things:
A sensor does not tell you what something is. It tells you a number. What that number means is your job to work out, in the place where the thing will actually be used.
That job has a name, calibration, and it is Part 8 of this band. Skip it and your night light will work perfectly on your desk and be useless in the room you built it for.
What you need
- Your board, USB cable and breadboard
- One potentiometer, the round knob with three legs
- One light sensor, a small round part with a wiggly line on top. It may be called an LDR or a photoresistor.
- One resistor of 10 000 ohms. The stripes read brown, black, orange.
- One LED and one 220 ohm resistor. A second LED, a second 220 ohm resistor, and a passive buzzer as well if you are doing the rain alarm or the room monitor Swap instead of the night guard: both of those use a green light and a red light, and the rain alarm beeps. The buzzer is not otherwise a Band 4 part; it arrives properly in Band 5.
- Jumper wires
By the end you will be able to
- Read a number from a sensor and say what range it can be in
- Explain how a knob turns a turning motion into a voltage
- Turn a number from one range into another with
map - Measure the same thing in three real places and write down what you found
- Say why
5 / 1023gives you zero, and fix it - Build something that decides for itself, using a limit that a person can set
Think first: the rope and the walk
15 minActivity one: the rope
You need something long and bendy again: a shoelace, a phone charger cable, string, or the rope from Band 1 with its knot untied.
Tie one end to something fixed, about level with your waist. A door handle, a chair leg, a table leg. Wrap it twice round and knot it so it will not slide.
Hold the other end in one hand and step back until the rope is straight. Not stretched tight, just not sagging.
Now pinch the rope with your free hand. Thumb on one side, one finger on the other, loose enough that the rope can still slide through your fingers.
Slide your pinching hand slowly from one end to the other, taking about five seconds for the whole journey. The rope itself stays still. Only your hand moves.
Answer these
- When your hand is right at the left-hand end, how much rope is on your left? How much on your right?
- When your hand is exactly in the middle, what fraction of the rope is on each side?
- As you slide from left to right, does the amount on your left change smoothly, or in jumps?
That is a potentiometer. The rope is a strip of material that resists electricity. The two ends are fixed. Your pinching hand is a contact that slides along it. Where your hand is decides how the total is split between the two sides.
Hold on to that. In Part 3 the two ends will be 5 volts and ground, and your hand will be the middle leg going to the board.
Activity two: the brightness walk
Take a pen and paper. Visit five different places, indoors and outdoors. For example: outside in the sun, outside in shade, by a window, in the middle of a room, and in a cupboard or under a cloth.
In each place, write down a number from 0 to 10 for how bright it feels, where:
- 0 means you cannot see your own hand in front of your face.
- 10 means it is so bright it hurts to look.
Write the two ends down at the top of your paper before you start, so you use the same scale in every place.
Then answer
- Is your "7" the same as your friend's "7"? How would you find out?
- If you did the same walk at eight in the evening instead, would your five numbers be the same?
- Somebody asks you to build a lamp that switches on "when it gets dark". What number would you tell it to use as the switching point, and how would you decide?
Keep this paper. In Part 8 you will do the exact same walk again, carrying the sensor, and compare your numbers with its numbers.
Question 3 has no right answer yet, and that is the point. Nobody can know the right switching number without going and measuring. Not you, not your facilitator, not the person who wrote the sensor.
What the board gives you
15 minThere is a second row of pins on your board, along the bottom, marked A0 to A5. The A stands for analog, which just means "a smooth amount rather than on or off".
These pins do something the digital pins cannot. They measure the voltage on the pin and give you a number for it.
The instruction is analogRead, and it works like this:
int reading = analogRead(A0);
The number you get back is always between 0 and 1023.
- 0 volts on the pin gives you 0
- 5 volts on the pin gives you 1023
- Anything in between gives you a number in between, in proportion
So 2.5 volts, exactly half, gives about 512.
Why 1023 and not 1000?
Band 2 met the same oddity with 255. It is the same reason.
The board splits the range into a fixed number of steps, and that number comes from how computers store things, not from anything convenient for humans. Here there are 1024 steps, counted from 0, so the last one is 1023.
Whenever you see a number like 255 or 1023, it came from the size of a box.
Words to know
- analog
- A smooth amount that can be anything in a range, rather than just on or off.
- voltage
- How hard the electricity is being pushed. Measured in volts.
- analogRead
- Measures the voltage on an analog pin and gives back a number from 0 to 1023.
- calibration
- Measuring, in the real place, what the sensor's numbers actually mean.
Do not confuse these two. analogWrite from Band 2 sends brightness out, and takes 0 to 255. analogRead here takes a measurement in, and gives 0 to 1023. Different jobs, different ranges, similar names. Write both in your glossary, side by side.
The knob, and how it makes a voltage
15 minA potentiometer is the rope from Part 1, built as a component. Take yours out and look at it. Three legs, and a shaft you can turn.
Inside there is a curved strip of resistive material. The two outer legs are joined to the two ends of that strip. The middle leg is joined to a contact that slides along it as you turn.
So when you turn the knob fully one way, the contact is right next to the 5 volt end, and the middle leg reads close to 5 volts, which is close to 1023. Turn it fully the other way and it sits next to ground, so close to 0 volts, which is 0. Anywhere between and you get a proportion.
This arrangement, where a voltage is split between two parts and you tap the middle, has a name: a voltage divider. Remember the name, because the light sensor in Part 7 is the same idea with one part replaced.
Words to know
- potentiometer
- A knob with three legs whose middle leg gives a voltage depending on the position of the knob. Often shortened to "pot".
- voltage divider
- Two resistances in a row across a supply, with a wire tapping the point between them.
Read the code before you run it
6 minint knobPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(knobPin);
Serial.println(reading);
delay(200);
}
Write your answers down first
- What is the smallest number you expect to see? What is the largest?
- How many numbers will appear each second?
- There is no
pinModeline forknobPin. Is that a mistake? - Notice that the pin is written
A0, with a capital A, and not just0. Why do you think that is?
Answers to 3 and 4, once you have written your guesses
3. Not a mistake. Analog pins used for reading do not need pinMode. You can write it and it does no harm, but it is not required.
4. Because there is already a pin called 0. Digital pin 0 and analog pin A0 are two different pins on two different edges of the board, and the capital A is how you say which one you mean.
Here is the honest detail, because it will save you an argument one day. For analogRead alone, the board is forgiving, and analogRead(0) happens to reach the same pin as analogRead(A0). But for pinMode, digitalRead and digitalWrite, A0 means pin 14 and 0 means pin 0, and those really are two different pins. Write the capital A every time and you never have to remember which instructions forgive you and which do not.
Build the knob and watch it
25 minBoth ends of every wire. Pulling a wire out of the breadboard and leaving its other end in the Arduino is the fault that catches everybody, because a leftover wire in an Arduino pin is invisible when you are looking at the breadboard. One Arduino pin takes one wire, so a wire you forgot will stop you dead later in the band and you will look for it on the board, where it is not. Band 3's red light is on pin 9, and Part 6 of this band needs pin 9.
And for this whole band, use the rails along the top edge for everything, the ones nearest row j. On most boards the top rails and the bottom rails are not joined to each other, so mixing them means nothing works and nothing looks wrong. Every build in this band puts its parts in rows f to j, so the top rails are the short journey.
Power the rails. Wire from the + rail to the 5V pin. Wire from the − rail to a GND pin.
This is the first build where the breadboard needs 5 volts of its own. Until now everything was powered straight from a digital pin.
Push the potentiometer into the board so its three legs land in f5, f6 and f7. Turn it so the shaft points away from the board, out over the edge, so you can reach it to turn it once everything is wired.
Hold the plastic body, line all three legs up, and press straight down.
It does not matter which way round the two outer legs go. Swapping them only reverses which way the numbers run as you turn. The middle leg is the one that matters, and it must be the middle one.
Wire from j5 to the + rail. That is one end of the rope.
Wire from j7 to the − rail. That is the other end.
Wire from j6 to pin A0. That is your hand on the rope, going to the board.
Plug in, upload b4_01_pot_read_given.ino, and open the serial monitor.
j5 and j7, and check the rails really do reach 5V and GND.Find the two ends. Turn the knob as far as it will go each way and write down the exact largest and smallest numbers you can reach.
Many knobs will not quite reach 0 or 1023. Yours might stop at 3 and 1020. That is normal and it is useful to know, because it means you should never write code that waits for exactly 1023.
map: turning one range into another
25 minh because the resistor already owns j18.Here is a problem you now have. Your knob gives you 0 to 1023. Brightness needs 0 to 255. They do not match.
You could do the arithmetic yourself. There is an instruction that does it for you:
int brightness = map(reading, 0, 1023, 0, 255);
Read it as a sentence: take reading, which lives on a scale from 0 to 1023, and give me the matching place on a scale from 0 to 255.
| Part | Means |
|---|---|
reading | the number you have |
0, 1023 | the scale it came from: lowest, then highest |
0, 255 | the scale you want: lowest, then highest |
So 0 becomes 0, 1023 becomes 255, and 512 becomes about 127.
Unplug the USB, then add a light. LED long leg into f20, short leg into f22. Resistor from j20 to j18. Wire h18 to pin 9 (row h, because j18 now holds the resistor leg and a hole takes one leg only). Wire j22 to the − rail.
Upload b4_02_pot_dim_led.ino and keep the serial monitor open.
Now try it backwards. Change the line to this and upload again:
int brightness = map(reading, 0, 1023, 255, 0);
Swapping the last two numbers flips the direction. That is a genuinely useful trick, and it is much easier than rewiring anything.
And try a small range. Change it to map(reading, 0, 1023, 60, 120); and upload.
What map does not do
It does not stop numbers going outside the range. If your reading is bigger than the number you promised, map will happily give you a result bigger than 255, and analogWrite will behave oddly.
There is a second instruction for that: constrain(value, 0, 255) forces anything below 0 up to 0 and anything above 255 down to 255. Wrapping one around the other is a common and sensible habit:
int brightness = constrain(map(reading, 0, 1023, 0, 255), 0, 255);
The light sensor
30 minTake out the light sensor. It is a small round part, usually orange or brown on top, with a wiggly line across its face.
It is a resistor whose resistance changes with light:
- Bright light makes it resist less.
- Darkness makes it resist much more.
It has no direction, so it goes in either way round.
Here is the difficulty
The board cannot measure resistance. It can only measure voltage. So a sensor that changes its resistance is no use on its own.
The fix is the voltage divider from Part 3. Put the sensor at the top and a fixed resistor at the bottom, and tap the middle. Now, as the sensor's resistance changes, the voltage at the middle changes, and the board can read that.
This is one of the most useful ideas in all of electronics. Almost every simple sensor you will ever meet is wired exactly this way.
Push the light sensor in so its two legs land in f12 and f14. Those holes are two columns apart, with column 13 empty between them.
Its legs come out much closer together than that, so spread them gently with your fingers first, holding the round body rather than the legs. It goes in either way round.
Wire from j12 to the + rail.
Put the 10 000 ohm resistor from j14 to j16. Its stripes read brown, black, orange. It is a different resistor from the 220 ohm ones. Check the stripes, do not guess.
Wire from h16 to the − rail. Row h, not row j: the resistor leg is in j16, and a hole takes one leg only. Same column, same connection.
Wire from h14 to pin A1. That is the middle point, between the sensor and the resistor.
Hole h14 is the same column as f14 and j14, so all three are the same connection. Any free hole in that column works.
Plug in, upload b4_03_light_read.ino, and open the serial monitor.
Write down four numbers: your reading with your hand fully covering the sensor, in the room normally, near a window, and with a torch on it.
Calibration: the most important part of this band
30 minYou now have a sensor giving numbers. Here is the question that decides whether your project works or not:
What number counts as "dark"?
Nobody can tell you. Not this page, not your facilitator, not the internet. It depends on your sensor, your resistor, the room, the time of day, and which way the sensor is pointing. Two people doing this band side by side will get different answers, and both will be right.
So go and find out.
Get your paper from Part 1, the one with your five places and your 0 to 10 guesses.
Take the board to each of those places with b4_03 running, and write the sensor's number next to your guess.
Two things that make this much easier
The sketch stays on the board when you unplug it. You proved that in Band 0. So you can unplug, walk somewhere, plug back into a laptop or a power bank there, and it is already running.
Point the sensor the same way every time. Straight up at the ceiling or the sky is easiest to repeat. If you point it at a window in one place and at the floor in the next, your five numbers are not comparable and the whole table is wasted.
Two ways round it, both fine:
The simple way. Do only the places you can reach with the laptop. Three real measurements beat five imagined ones, and the rest of the band works perfectly with three rows in the table.
The clever way. Make the board report without a screen, by blinking the light that is already soldered onto it. Open
b4_03_light_read.ino and replace the whole of loop with this, then upload:void loop() { int reading = analogRead(lightPin); int blinks = reading / 100; for (int i = 0; i < blinks; i = i + 1) { digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(300); } delay(2000);}Add
pinMode(13, OUTPUT); to setup as well. Now a reading of 380 gives three blinks and 720 gives seven. Take the board and a power bank to the room, watch, count, come back.That
reading / 100 throws the remainder away, which is exactly the trap you meet in Part 9. Here it is doing precisely what you want, and it is worth remembering that the same behaviour can be a bug in one place and the whole point in another. It is rough, and rough is enough: you are looking for “about 400 in here and about 800 out there”, not exact numbers.If carrying a laptop about is awkward, do it in whatever three places you can reach. Three real measurements beat five imagined ones.
Make a table like this and fill it in properly.
| Place | My guess, 0 to 10 | Sensor number | Time of day |
|---|---|---|---|
| Outside in the sun | |||
| Outside in shade | |||
| By a window | |||
| Middle of a room | |||
| Under a cloth |
Now choose your number. Look at your table and pick the reading that sits between "light enough to see" and "dark enough to want a lamp". That is your switching point.
Write it down with a reason. Not just "400", but "400, because the middle of the room read 520 at six in the evening and 310 once the sun had gone."
From here on this page calls that number the threshold. It is just a shorter word for the switching point: the number the sensor has to cross before the device changes its mind.
Two things your table will show you
Your guesses and the sensor's numbers do not line up neatly. Your eyes adjust as you walk about. The sensor does not. A room that feels "about half as bright" as outside will usually read far less than half.
The time of day matters enormously. This is why the Make task in this band puts the switching point on a knob instead of writing it into the code. A number written into the code is a decision made once, by you, in one room, on one day. A knob lets the person using it decide, in their room, whenever they like.
That is a design idea, not an electronics one, and it is worth more than either.
The division trap
20 minSuppose you want to turn your reading into actual volts. The arithmetic is easy. 1023 stands for 5 volts, so:
volts = 5 / 1023 * reading
Upload b4_04_buggy_intdiv.ino, which contains exactly that line, and watch the serial monitor.
0.00. Always. Whatever you do.Nothing is broken. The arithmetic is correct. The problem is what kind of numbers the board thinks it is dealing with.
Whole numbers and decimals are different types
You have been using int since Band 0. An int holds a whole number only. It has no way to hold a fraction.
There is another type, float, which can hold decimals like 0.00489 or 3.5.
Here is the trap. When the board sees 5 / 1023, both of those are whole numbers, so it does whole-number division. The true answer is about 0.00489. There is nowhere to put the fraction, so it is thrown away, and the answer becomes 0.
Then it multiplies your reading by 0. And 0 times anything is 0.
Saying float volts does not help. By the time the answer is stored, the damage is already done.
The fix
Write at least one of the numbers with a decimal point. That tells the board to work in decimals from the start:
float volts = 5.0 / 1023.0 * reading;
Make that change and upload.
Now try just one of them. Change it to 5.0 / 1023 and upload. It still works.
One decimal point anywhere in the division is enough. The board sees a decimal on one side and works in decimals for the whole thing.
Now try a different fix, with no decimals at all. Replace the line with these two:
int millivolts = 5000 * (long)reading / 1023;
Serial.println(millivolts);
This works because of the order. Multiplying by 5000 first makes the number big, and only then does the division throw a fraction away. Losing a fraction out of 5000 hardly matters. Losing one out of 5 loses everything.
(long) is doing5000 times a reading of 1023 is over five million, which is too big for an ordinary int. Writing (long) in front tells the board to use a bigger box for the arithmetic. Leave it out and the answer wraps round and goes nonsense above a reading of about 6, which is almost straight away. Try it if you like, then put it back.Both fixes are used in real code. Decimals are easier to read. Whole numbers are faster and use less memory, which matters on a small chip. What decides between them is whether the scale you choose is big enough to hold the detail you need.
Words to know
- int
- A name that holds a whole number. No fractions.
- float
- A name that can hold a number with a decimal point.
- whole-number division
- When both numbers are whole, the fraction in the answer is thrown away, not rounded.
Build the night guard
30 minEverything is already wired. The knob is on A0, the light sensor is on A1, the LED is on pin 9. You only need the sketch.
Upload b4_05_night_guard_worked.ino and open the serial monitor.
Read the sketch
void loop() {
int light = analogRead(lightPin);
int threshold = analogRead(knobPin);
if (light < threshold) {
digitalWrite(lampPin, HIGH);
} else {
digitalWrite(lampPin, LOW);
}
...
}
That is the whole idea in four lines. Two sensors, one comparison, one decision.
Notice what the knob is really doing. It is not controlling the light. It is setting the number that counts as dark. The person using the device gets to make that decision, in their room, at their time of day, without touching any code.
Now find the flaw
Turn the knob very slowly until the lamp just starts to twitch, right on the point of coming on. Then take your hand off the knob completely, because your fingers will keep nudging it, and watch for thirty seconds.
This is a real problem and every device that compares against a limit has it. Fixing it is the Gold task in Part 12, and the fix has a name you can look up: hysteresis. It means using two limits instead of one, with a gap between them.
Finding a flaw in something that works is a good day's work. Most people never look for one.
Look closer
25 mina. Work it out by hand
Fill in this table without running anything. Assume the knob and sensor are wired as in this band.
| Sensor reading | Knob reading | Is light < threshold? | Lamp |
|---|---|---|---|
| 800 | 400 | no | off |
| 200 | 400 | ||
| 400 | 400 | ||
| 399 | 400 | ||
| 200 | 0 | ||
| 1000 | 1023 |
Check your table
200 and 400: yes, lamp on. 400 and 400: no, lamp off, because < means strictly less than and they are equal. 399 and 400: yes, lamp on, by one. 200 and 0: no, lamp off, because nothing is less than 0, so turning the knob all the way down switches the lamp off permanently. 1000 and 1023: yes, lamp on, so turning the knob all the way up leaves the lamp on almost all the time.
Those last two rows are worth noticing. The two ends of the knob are "never on" and "almost always on". That is a sensible design: the person can switch the whole thing off, or force it on, using the same control.
b. Break it on purpose
In b4_05, change if (light < threshold) to if (light > threshold). Predict what will happen before you upload.
Answer
It becomes a day lamp. It switches on when the room is bright and off when it is dark, which is exactly wrong for a night light and exactly right for something else, like a warning that a fridge door has been left open.
One character reverses the whole purpose of the device. Change it back.
c. Put the lines back in order
These lines have been shuffled:
analogWrite(lampPin, brightness);
int brightness = map(reading, 0, 1023, 0, 255);
int reading = analogRead(knobPin);
Put them in a working order and say why no other order works.
Answer
Read the knob, turn the reading into a brightness, then send the brightness out. No other order works because each line needs the name that the line before it created. You cannot map a reading you have not taken, and you cannot send a brightness you have not worked out.
d. Explain one line
In b4_05, the same comparison light < threshold appears twice: once to decide the lamp, and once again lower down to decide what to print. Why not just print inside the first if?
Answer
You could, and it would work. The reason it is written this way is that the printing block is deliberately separate: it always prints the same three things in the same order, so the output is easy to read and easy to compare between runs.
But you have spotted something real. The same test written twice means that if you ever change one and forget the other, the display will lie about what the lamp is doing. That is a genuine weakness, and the tidy fix uses a tool you meet in Band 5: work the answer out once, keep it in a name, and use that name in both places.
Full marks for anything that notices the duplication is a risk.
Change it
30 minWork on a copy of b4_05.
- Bronze
- Make the lamp fade as the light falls, instead of snapping on. The darker it gets, the brighter the lamp. You will need
mapandanalogWrite, and you will need to think about which way round the map goes. - Silver
- Print the reading in volts as well as as a number, using what you learned in Part 9. Then check it: at the halfway point on the knob, does it really say about 2.5?
- Gold
- Fix the flickering you found in Part 10. Use two limits instead of one: switch the lamp on when the light falls below the threshold, but do not switch it off again until the light rises above the threshold plus 50. Then find the smallest gap that stops the flicker, and write that number in your log.
A hint for Bronze
A low sensor reading means dark, and dark should mean bright lamp. So the two ranges run in opposite directions. Look again at the reversed map you tried in Part 6, step 3.
A hint for Gold, after you have tried it
You need the board to remember whether the lamp is currently on, because the rule now depends on that. A bool from Band 3 is exactly the tool.
If the lamp is off, switch on when light < threshold. If the lamp is already on, switch off only when light > threshold + gap. Two different tests, chosen by what the lamp is already doing.
Make: something that watches and decides
100 minWhat to build
A device that measures something about the world, compares it against a limit a person can set, and does something about it. Pick one, or bring your own:
- A security light for a corridor or a doorway
- A light that warns you when your room is too dark to read in
- A plant monitor: has this plant had enough light today?
- A cupboard alarm: a light comes on when the door is opened and light gets in
- A study lamp that gets brighter as the room gets darker, so the total stays even
It is finished when all of these are true
- The person using it can change the limit without touching any code
- You have a calibration table with at least three real measurements, taken where the device will actually live
- It does not flicker when the reading sits near the limit
- The serial monitor shows enough for somebody else to work out what it is thinking
- It still behaves sensibly when you take it into a completely different room
The test that matters most in this band
Set it up carefully where you built it, and check it works. Then carry it to a different room and switch it on without changing anything.
You do not need a laptop to do this. A phone charger or a power bank plugged into the same USB socket runs the board perfectly well, and plugging it in is switching it on. The sketch is already inside the board.
Write down what happened. Did it still make sense? Did the person have to reach for the knob straight away? Was the knob even in a place they could reach?
Almost every sensor project in the world fails this test the first time. Yours failing is normal and expected. What you write down about why is the actual work.
Show your work
Post a video, your calibration table, and one paragraph on what happened when you moved it. Then look at one other person's calibration table and ask them one question about how they chose their number.
Check yourself
15 minCode you have not seen. Answer from reading only.
int sensorPin = A2;
int lampPin = 6;
void setup() {
pinMode(lampPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int reading = analogRead(sensorPin);
int level = map(reading, 0, 1023, 0, 100);
float share = level / 100;
analogWrite(lampPin, level);
Serial.println(share);
delay(100);
}
- What range of numbers can
readingbe in? - What range can
levelbe in? - The lamp never gets close to full brightness. Why not?
- What will
shareprint, for every reading except the very highest? Explain. - Fix line by line: what is the smallest change that makes
sharework properly? - The sketch has no wire connected to pin A2 at all. What will the serial monitor show, and what is that called?
Answers
1. 0 to 1023.
2. 0 to 100, because that is what the map was told to produce.
3. Because analogWrite wants 0 to 255, and level never goes above 100. So the lamp tops out at well under half brightness. The map should have gone to 255, not 100. Mixing up the two ranges is the classic Band 4 mistake, and this is it in the wild.
4. 0.00 every time, except when level is exactly 100, when it gives 1.00. Both level and 100 are whole numbers, so the division throws the fraction away. Writing float share does not save it, because the damage happens before the answer is stored.
5. Put a decimal point on the 100: float share = level / 100.0;. One character.
6. Numbers drifting and jumping about with no pattern, and changing if you wave your hand near the board. It is called a floating pin, exactly the same fault as the button in Band 3. An analog pin with nothing connected has no more idea what it is reading than a digital one does. This is the question that matters most in this band. If you answered "0", check what you saw in Band 3 Part 4.
When something goes wrong
| What you see | What it usually is | What to do |
|---|---|---|
| The number never changes | One of the outer legs is not connected | Check both ends of the divider reach the rails, and that the rails reach 5V and GND. |
| The number jumps about on its own | The middle leg is not reaching the analog pin | A floating analog pin, just like a floating button pin. Check that one wire. |
| The sensor barely changes with light | Wrong fixed resistor | You need 10 000 ohms, striped brown black orange. A 220 ohm one gives almost no swing. |
| Bigger number when it gets darker | Sensor and resistor are swapped | Not wrong, just upside down. Swap them, or write down that for you a big number means dark. |
| Any calculation gives 0.00 | Whole-number division | Put a decimal point on one of the numbers. See Part 9. |
| The lamp is very dim at full | You mapped to 100 or 1023 instead of 255 | analogWrite wants 0 to 255. Check the last two numbers in your map. |
| The lamp flickers near the switching point | Nothing is wrong. This is normal | It needs two limits with a gap. See Part 12, Gold. |
A digitalWrite or pinMode acts on the wrong pin | You wrote 0 instead of A0 | For those instructions A0 is pin 14 and 0 is pin 0. (analogRead forgives you and treats them the same, which is why this only bites later.) Always write the capital A. |
| Everything was fine, now nothing works | The rail wire to 5V or GND has come out | This build needs power on the breadboard. Check both rail wires first. |
Still stuck after 30 minutes?
Photograph the board from above, and copy ten lines from your serial monitor. Post both, plus one sentence on what you expected. In this band the serial numbers are usually enough on their own for someone to tell you what is wrong.
What you know now
Your board can measure the world now, not just be poked at. And you know the thing that separates a project that works on a desk from one that works in a room: the number a sensor gives you is not a fact about the world, it is a reading that has to be interpreted, in the place where it will be used.
You also met the division trap, which will catch you again one day. When a calculation gives you a stubborn zero, look at the types first.
Before you move on, make sure you have
- Your working device, with a video
- Your calibration table with at least three real measurements
- Your paragraph on what happened when you moved it to another room
- The number you found for Gold, if you did it
- Both
analogReadandanalogWritein your glossary, with their different ranges - Your build log for this band
- Five or six correct answers in Part 14, including question 6
Next is Band 5: Simon Says. It is the hardest band so far and the most important one. You will write your own instructions for the first time, and you will find out why delay, which you have used in every single band, has been quietly holding you back the whole time.
Ship it
One video under sixty seconds, and one post of five lines. The video is the same one this band already asks you for as evidence, so this is not extra work. It is the same work, done once, somewhere a person can see it.
The four shots: three seconds of the thing sitting still, fifteen of you doing something to it, fifteen of it responding all the way to the end, and ten of the honest bit, which is your measurement or the wire that was wrong the first time.
The line worth writing in the post: your two threshold numbers and how you went and measured them.
Show your work has the five-line template, a
worked example, and the three checks to make before anything goes public. Tag it
#BozomaBuilds so all nine of yours sit together.