blocks / nano-light-comparator
Light comparator on a Nano
Move the light slider. In the dark the LED lights: an LDR and a trimmer set a threshold, an LM358 lights a red LED, and a Nano on D2 prints dark or light.
on Nano (CH340 clone)10 parts1 test
orbe add nano-light-comparator
Loading the Block
Move the light slider. In the dark the LED lights: an LDR and a trimmer set a threshold, an LM358 lights a red LED, and a Nano on D2 prints dark or light.
const int sensePin = 2;
int last = -1;
int reports = 0;
void report(int high) {
if (high == last) {
return;
}
last = high;
reports++;
Serial.print(high ? "dark" : "light");
Serial.print(' ');
Serial.println(reports);
}
void setup() {
pinMode(sensePin, INPUT);
Serial.begin(115200);
report(digitalRead(sensePin) == HIGH);
}
void loop() {
report(digitalRead(sensePin) == HIGH);
}
// An LDR divider and a trimmer divider feed an LM358 on a full-size breadboard beside a Nano.
// The Nano straddles the center channel over columns 1 to 15. D2 is j11, 5V is a12 and GND is a14.
// The LM358 sits with pin 1 (OUT1) in e28, so IN1- is e29, IN1+ is e30, GND is e31, and VCC is f28.
// Dark makes the LDR node fall below the trimmer, the output goes high, and the LED lights.
{
"format": 1,
"parts": {
"bb": { "type": "breadboard-830" },
"nano": { "type": "nano-board", "firmware": "build/light.ino.hex" },
"u1": { "type": "lm358-dip8" },
"ldr": { "type": "ldr-5mm" },
"trim": { "type": "trimmer-3362p", "resistance": "10k", "position": "0.5" },
"rbot": { "type": "resistor-axial", "resistance": "2.2k" }, // under the LDR, so the node rises with the light
"rset": { "type": "resistor-axial", "resistance": "4.7k" }, // above the trimmer, so its top stays under 3.5 V
"rhys": { "type": "resistor-axial", "resistance": "68k" }, // output back to IN1+, so the threshold snaps
"rled": { "type": "resistor-axial", "resistance": "220" },
"led1": { "type": "led-5mm" },
},
"layout": {
"bb": { "at": [0, 0, 0] },
"nano": { "on": { "D13": "bb.d1" } },
"u1": { "on": { "OUT1": "bb.e28" } }, // IN1- e29, IN1+ e30, GND e31, VCC f28, OUT2 f29, IN2- f30, IN2+ f31
"ldr": { "on": { "a": "bb.c21" } }, // b in c22
"rbot": { "on": { "a": "bb.b22" }, "rotate": [0, 0, -90] }, // b in X22, the - rail
"rset": { "on": { "a": "bb.a40" }, "rotate": [0, 0, -90] }, // b in W40, the + rail
"trim": { "on": { "a": "bb.c40" } }, // b in c42, the wiper in d41
"rhys": { "on": { "a": "bb.b28" } }, // b in b32; c32 jumpers that strip back to IN1+
"rled": { "on": { "a": "bb.d24" } }, // b in d28, OUT1
"led1": { "on": { "anode": "bb.c24" } }, // cathode in c25
},
"nets": { "GND": "nano.GND" },
"world": { "light": 300 },
"wires": [
["bb.a12", "bb.W11", { "color": "#d12b2b" }], // 5V to the + rail beside a
["bb.W12", "bb.Y12", { "color": "#d12b2b" }], // the same rail beside j, for VCC
["bb.a14", "bb.X13", { "color": "#161616" }], // GND to the - rail beside a
["bb.X61", "bb.Z61", { "color": "#161616" }], // the same rail beside j
["bb.a21", "bb.W21", { "color": "#d12b2b" }], // ldr.a to 5V
["bb.a22", "bb.a29"], // ldr.b and rbot.a to IN1-
["bb.a42", "bb.X42", { "color": "#161616" }], // trim.b to GND
["bb.a41", "bb.a30"], // trim.wiper to IN1+
["bb.c32", "bb.c30"], // rhys.b to IN1+, beside the resistor
["bb.a25", "bb.X25", { "color": "#161616" }], // led1.cathode to GND
["bb.j28", "bb.Y28", { "color": "#d12b2b" }], // VCC to the + rail, off the pin
["bb.a31", "bb.X31", { "color": "#161616" }], // u1.GND to the - rail, off the pin
["bb.j31", "bb.Z31", { "color": "#161616" }], // IN2+ to the - rail beside j
["bb.j30", "bb.Z30", { "color": "#161616" }], // IN2- to that rail, so the unused channel has a DC path
["bb.j11", "bb.j20"], // D2 across to an empty column
["bb.i20", "bb.b20"], // over the channel, one hole in from the runs
["bb.a20", "bb.a28"], // to OUT1, which is on the a to e side
],
}
# The Nano's 5V pin is 4.76 V on USB, one Schottky drop under the host's 5.0 V. On a typical USB # port a real board reads 4.55 V to 4.8 V, and each band in volts holds over that range. The # dividers scale with the pin, so where the LED switches does not move. # At 300 lx the LDR node is 2.52 V and the threshold is 1.54 V, so the LED is off. open step 50ms expect @led1 off expect @nano.D2 low expect @nano.D2 < 0.2V expect @u1.IN1_N > 2.40V expect @u1.IN1_N < 2.56V expect @u1.IN1_P > 1.46V expect @u1.IN1_P < 1.57V expect @u1.OUT1 < 0.008V expect serial contains "light 1" # 80 lx is between the two thresholds, so the output stays low for this whole step. set light 80 step 200ms expect @led1 off expect @nano.D2 < 0.2V expect @u1.IN1_N > 1.53V expect @u1.IN1_N < 1.63V # 10 lx puts the node at 0.61 V, under both thresholds, so the LED comes on. set light 10 step --until @led1 lit --timeout 100ms step 50ms expect @led1 lit expect @nano.D2 high expect @nano.D2 > 3.0V expect @u1.OUT1 > 3.14V expect @u1.OUT1 < 3.39V expect @u1.IN1_N > 0.575V expect @u1.IN1_N < 0.620V expect @u1.IN1_P > 1.61V expect @u1.IN1_P < 1.72V expect serial contains "dark 2" # The same 80 lx, reached from the dark, keeps the LED on. set light 80 step 200ms expect @led1 lit expect @nano.D2 > 3.0V expect @u1.IN1_P > 1.61V expect @u1.IN1_P < 1.72V set light 300 step --until @led1 off --timeout 100ms step 50ms expect @led1 off expect @nano.D2 low expect @nano.D2 < 0.2V expect serial contains "light 3" # The trimmer sets the threshold. Turned toward `a`, to 0.1, it raises the threshold to 2.93 V, # over the LDR node's 2.52 V at 300 lx, so the LED lights in the same light. Turned back, it goes out. set @trim.position 0.1 step --until @led1 lit --timeout 100ms step 50ms expect @nano.D2 high expect @u1.IN1_P > 2.85V expect serial contains "dark 4" set @trim.position 0.5 step --until @led1 off --timeout 100ms step 50ms expect @nano.D2 low expect serial contains "light 5" expect no diagnostics
orbe add writes these files.
How it works
An LDR and a trimmer set a threshold on an LM358. In the dark the output goes high, a red LED lights, and the Nano prints dark and a count. In the light the LED is off and the Nano prints light and a count. The sketch prints once at the start and again only when D2 changes. A run that goes dark and comes back prints light 1, then dark 2, then light 3.
The LDR sits above 2.2k to ground, so the node rises as the room gets brighter. It is 2.52 V at the scene's 300 lx.
orbe set light 10 darkens the room and the LED comes on. orbe set light 300 brings the room back and the LED goes out.
The trimmer sets the threshold, 1.54 V at position 0.5. orbe set @trim.position 0.1 turns it toward a and raises the threshold to 2.93 V, over the LDR node, so the LED comes on in the same light. On the 3D page the trimmer has a slider beside the light's.
Check it with orbe check scene.jsonc and run its tests with orbe test.
Parts in this circuit
- nanoNano (CH340 clone)light.ino.hex
- trimTrimmer potentiometer, 1/4 inch square (3362P)10 kΩ, position 0.5
- rbotResistor, 1/4 W axial2.2 kΩ
- rsetResistor, 1/4 W axial4.7 kΩ
- rhysResistor, 1/4 W axial68 kΩ
- rledResistor, 1/4 W axial220 Ω
- led15 mm LED, red
License and files(MIT OR Apache-2.0) AND LGPL-2.1-or-later
| File | Size |
|---|---|
| README.md | 974 B |
| block.jsonc | 539 B |
| build/light.ino.hex | 6.2 kB |
| firmware/LICENSE-libc | 2.5 kB |
| firmware/LICENSE-runtime | 26.4 kB |
| firmware/NOTICE | 1.6 kB |
| firmware/light/light.ino | 408 B |
| scene.jsonc | 3.2 kB |
| tests/light.test | 2.0 kB |