Heat, magnet and wheel, on an Arduino Uno
An LM35 on A0, an A3144 Hall switch on pin 2 and a slotted photo interrupter counting a 20-slot wheel on pin 3, read on the serial monitor. The whole build, an Arduino Uno and 3 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.
BeginnerRuns in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Heat, magnet and wheel: three kit sensors that each read a different way.
//
// A0 the LM35's middle leg; its outer legs go to 5V and GND
// pin 2 the A3144's OUT, with the Uno's own pull-up (INPUT_PULLUP); the
// sensor runs from IOREF, which is 5 V on an Uno
// pin 3 the photo interrupter's S, counted by interrupt; the board runs
// from 3V3, and its 3.3 V high is still a HIGH to a 5 V Uno
// pin 13 the built-in LED, lit while the magnet is there
//
// # The LM35: a voltage
//
// Ten millivolts a degree from 0 V at 0 C, with no offset. The ADC gives
// 0 to 1023 against 5 V, so millivolts = reading x 5000 / 1024, which is
// exactly reading x 625 / 128, and one millivolt is a tenth of a degree. No
// floating point anywhere. An LM35 needs 4 V or more, which is why it is on
// 5 V here and why a 3.3 V board wants a TMP36 instead.
//
// # The A3144: an open collector
//
// It only ever pulls OUT down, so the pin needs a pull-up and reads LOW while
// a south pole is near its printed face. Turn the magnet round and nothing
// happens: it is unipolar.
//
// # The photo interrupter: pulses
//
// S goes HIGH each time a spoke of the wheel cuts the beam. Counting rising
// edges for a second gives slots a second, and the disc has 20 slots, so
// RPM = pulses x 60 / 20 = pulses x 3.
const int LM35_PIN = A0;
const int HALL_PIN = 2;
const int SLOT_PIN = 3;
const int LAMP = 13;
const int SLOTS = 20;
volatile unsigned int pulses = 0;
void countSlot() {
pulses++;
}
/** Sixteen conversions, averaged, as tenths of a degree. */
int readTenths() {
unsigned int total = 0;
for (int i = 0; i < 16; i++) {
total += (unsigned int)analogRead(LM35_PIN);
}
long mean = total >> 4;
return (int)(mean * 625 / 128);
}
void setup() {
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: LM35, A3144 and a slotted wheel");
pinMode(HALL_PIN, INPUT_PULLUP);
pinMode(SLOT_PIN, INPUT);
pinMode(LAMP, OUTPUT);
attachInterrupt(digitalPinToInterrupt(SLOT_PIN), countSlot, RISING);
}
void loop() {
static unsigned long last = 0;
bool magnet = digitalRead(HALL_PIN) == LOW;
digitalWrite(LAMP, magnet ? HIGH : LOW);
if (millis() - last >= 1000) {
last += 1000;
noInterrupts();
unsigned int counted = pulses;
pulses = 0;
interrupts();
int tenths = readTenths();
Serial.print("temperature ");
Serial.print(tenths / 10);
Serial.print(".");
Serial.print(tenths % 10);
Serial.print(" C magnet ");
Serial.print(magnet ? "yes" : "no ");
Serial.print(" wheel ");
Serial.print((long)counted * 60 / SLOTS);
Serial.println(" RPM");
}
}
Parts list
4 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
9 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; Photo interrupter, slot pin GND
- Arduino Uno R3 pin 3; Photo interrupter, slot pin SIG
- Arduino Uno R3 pin 2; Hall sensor, A3144 pin OUT
- Arduino Uno R3 pin IOREF; Hall sensor, A3144 pin VCC
- Arduino Uno R3 pin 3V3; Photo interrupter, slot pin VCC
- Arduino Uno R3 pin 5V; Temperature sensor, LM35 pin VS
- Ground: Arduino Uno R3 pin GND; Temperature sensor, LM35 pin GND
- Ground: Arduino Uno R3 pin GND; Hall sensor, A3144 pin GND
- Arduino Uno R3 pin A0; Temperature sensor, LM35 pin VOUT
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.