KY comparator boards and an IR switch on an Arduino Uno
A KY-024 Hall, KY-025 reed, KY-028 thermistor and KY-037 sound board on pins 2 to 5, D0 high past the trimmer, and an E18-D80NK pulling pin 6 low. The whole build, an Arduino Uno and 5 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $41. On Mokxi they're free.
What you'll learn
- Digital outputs from comparator sensor boards
- Printing only when a state changes
- Holding a noisy signal for a short time
- Active low sensors and
INPUT_PULLUP
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 7
How it works
Four KY boards each have a sensor and a comparator, and send a digital D0 signal to pins 2 to 5: Hall on 2, reed on 3, thermistor on 4, sound on 5. D0 goes HIGH when the sensor passes the level set by the board's trimmer.
The sketch watches for a change on each pin and prints a line such as field on or warm off. The sound board gives a burst of pulses on a clap, so the sketch holds loud for 500 ms after the last one. The E18-D80NK on pin 6 is the other way around: it pulls the pin LOW when something is near.
Parts list
7 parts, plus the jumper wires. They retail for about $41; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × Linear Hall sensor, KY-024
- 1 × Reed switch module, KY-025
- 1 × Digital temperature module, KY-028
- 1 × Big sound sensor, KY-037
- 1 × IR proximity switch, E18-D80NK
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Linear Hall sensor, KY-024 pin GND | Arduino Uno R3 pin GND |
| Reed switch module, KY-025 pin GND | Arduino Uno R3 pin GND |
| Digital temperature module, KY-028 pin GND | Arduino Uno R3 pin GND |
| Big sound sensor, KY-037 pin GND | Arduino Uno R3 pin GND |
| IR proximity switch, E18-D80NK pin GND | Arduino Uno R3 pin GND |
| IR proximity switch, E18-D80NK pin OUT | Arduino Uno R3 pin 6 |
| Big sound sensor, KY-037 pin DO | Arduino Uno R3 pin 5 |
| Digital temperature module, KY-028 pin DO | Arduino Uno R3 pin 4 |
| Reed switch module, KY-025 pin DO | Arduino Uno R3 pin 3 |
| Linear Hall sensor, KY-024 pin DO | Arduino Uno R3 pin 2 |
| Linear Hall sensor, KY-024 pin VCC | Arduino Uno R3 pin 5V |
| Reed switch module, KY-025 pin VCC | Arduino Uno R3 pin 5V |
| Digital temperature module, KY-028 pin VCC | Arduino Uno R3 pin 5V |
| Big sound sensor, KY-037 pin VCC | Arduino Uno R3 pin 5V |
| IR proximity switch, E18-D80NK pin VCC | Arduino Uno R3 pin 5V |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Four KY comparator boards on pins 2 to 5, and an E18-D80NK on pin 6.
//
// KY-024 Hall D0 -> pin 2 KY-025 reed D0 -> pin 3
// KY-028 thermistor D0 -> pin 4 KY-037 sound D0 -> pin 5
// E18-D80NK black (OUT) -> pin 6
// every + (brown) -> 5V every G (blue) -> GND
//
// On these boards D0 goes HIGH, and the board's own lamp lights, when the
// sensor passes the trimmer: a south pole at the Hall sensor, a magnet at the
// reed, a warm thermistor, a loud sound. The sound board's D0 is a burst of
// short pulses on a clap, so the sketch holds "loud" for half a second after
// the last one.
//
// The E18-D80NK is the other way around: its NPN open collector pulls pin 6
// LOW while something is within its range and drives nothing otherwise, so
// pin 6 needs a pull-up, and INPUT_PULLUP gives it one.
//
// Drag the magnets, the temperature and the target, and clap.
const int pins[] = {2, 3, 4, 5};
const char *names[] = {"field", "magnet", "warm", "loud"};
int last[] = {-1, -1, -1, -1};
int lastNear = -1;
unsigned long heard = 0;
void setup() {
for (int i = 0; i < 4; i++) pinMode(pins[i], INPUT);
pinMode(6, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Mokxi Uno: KY comparator boards on 2 to 5, IR switch on 6");
}
void loop() {
if (digitalRead(5) == HIGH) heard = millis();
for (int i = 0; i < 4; i++) {
int now = digitalRead(pins[i]) == HIGH ? 1 : 0;
if (i == 3) now = heard != 0 && millis() - heard < 500 ? 1 : 0;
if (now != last[i]) {
last[i] = now;
Serial.print(names[i]);
Serial.println(now ? " on" : " off");
}
}
int near = digitalRead(6) == LOW ? 1 : 0;
if (near != lastNear) {
lastNear = near;
Serial.println(near ? "something there" : "clear");
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Makes pins 2 to 5 inputs for the four boards. Pin 6 is set to INPUT_PULLUP, because the E18-D80NK only pulls its output low and drives nothing otherwise. It then opens serial and prints a hello line.
void setup() {
for (int i = 0; i < 4; i++) pinMode(pins[i], INPUT);
pinMode(6, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Mokxi Uno: KY comparator boards on 2 to 5, IR switch on 6");
}loop sketch.ino
Notes the time whenever the sound pin is HIGH. For each board it reads its pin, treats sound as on for 500 ms after the last pulse, and prints only when the state changed. Pin 6 is checked last, where LOW means something is there.
void loop() {
if (digitalRead(5) == HIGH) heard = millis();
for (int i = 0; i < 4; i++) {
int now = digitalRead(pins[i]) == HIGH ? 1 : 0;
if (i == 3) now = heard != 0 && millis() - heard < 500 ? 1 : 0;
if (now != last[i]) {
last[i] = now;
Serial.print(names[i]);
Serial.println(now ? " on" : " off");
}
}
int near = digitalRead(6) == LOW ? 1 : 0;
if (near != lastNear) {
lastNear = near;
Serial.println(near ? "something there" : "clear");
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The sound board prints on and off many times This project
That is the burst of pulses. The 500 ms hold smooths it. Raise it if it still flickers. - The IR switch seems backwards This project
It is active low: LOW means something is near and HIGH means clear. - A board never turns on This project
Turn its trimmer, and check VCC to 5V, GND to ground and D0 to the right pin. - The analog output barely changes. Linear Hall sensor
The sensor sits around half the supply with no magnet, and moves up or down with the pole and distance. Read AO with analogRead and watch for small changes. - The digital output does not trigger on both poles. Linear Hall sensor
DO reacts to one pole only and trips past the trimmer setting. Turn the trimmer to adjust the threshold, and flip the magnet to see the other pole. - The module output never changes. Reed switch module
Use DO for on and off, and tune the blue trimmer to set the threshold. AO gives an analog level. Bring the magnet close to the glass reed. - The logic is the opposite of what you expect. Reed switch module
Check the LED on the board and whether DO goes high or low with the magnet. Power it with 3.3 V or 5 V and join the grounds. - The analog reading looks odd. Digital temperature module
AO falls as the temperature rises, since the NTC thermistor is in a divider. Convert it with the Steinhart-Hart or beta formula. It is not a calibrated sensor.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.