← All lessons
How embedded systems work
Read, decide, write, repeat
Sensor
Chip
Fan
fan.ino
const int SENSOR = A0; // temperature sensorconst int FAN = 9; // fan, through a transistor void setup() { pinMode(FAN, OUTPUT);} void loop() { int raw = analogRead(SENSOR); float volts = raw * 5.0 / 1024; float temp = (volts - 0.5) * 100; bool hot = temp > 28; digitalWrite(FAN, hot ? HIGH : LOW); delay(1000);}A limit flips the pin and a gap keeps it calm
analogRead(A0)
pin 9
fan
fan.ino
// one limitif (temp > 28) fan = HIGH;else fan = LOW; // two limits, with a gap between themif (temp > 29) fan = HIGH;else if (temp < 27) fan = LOW;// from 27 to 29: leave it as it was digitalWrite(FAN, fan);A quick press can slip past the loop unless the pin interrupts it
Button pin over 4 seconds
polling.ino
void loop() { if (digitalRead(BUTTON) == LOW) { presses++; } delay(1000); // not looking here}interrupt.ino
volatile int presses = 0; // runs the moment the pin fallsvoid onPress() { presses++;} void setup() { attachInterrupt(digitalPinToInterrupt(BUTTON), onPress, FALLING);} void loop() { delay(1000); // busy, and that's fine}Everything fits in 2 KB or the chip resets
RAM on an Arduino Uno 2,048 bytes
Next to bigger computers
RAMFlashClock
Arduino Uno2 KB32 KB16 MHz
ESP32520 KB4 MB240 MHz
Laptop16 GB512 GB3 GHz × 8
A laptop has about 8 million times the RAM of an Uno
history.ino
// Arduino Uno: 2,048 bytes of RAM in allfloat history[N]; // 4 bytes per readingint next = 0; void loop() { history[next] = readTemp(); next = (next + 1) % N;} // out of RAM? there's no error message// the stack runs into history, the chip resetsTopics
Next: How RAG works
Open lesson