Smart Home in the Classroom with Arduino
FunduinoPublished on
Das Smart Home Education System – Aufbau, 16 Module und Home Assistant
Hardware overview
1 / 2
Setup and preparation
Set up the Arduino IDE for the ESP32
Module 1: Red LED
Make a red LED blink
// Auf der Platine ist die rote LED mit dem digitalen Pin 25 des ESP32 verbunden.
int ledPin = 25;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH); // LED an
delay(1000); // 1 Sekunde warten
digitalWrite(ledPin, LOW); // LED aus
delay(1000); // 1 Sekunde warten
}Module 2: Green LED
Make the green LED blink
// Die grüne LED hängt an Pin 26 statt an Pin 25.
int ledPin = 26;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH); // LED an
delay(1000); // 1 Sekunde warten
digitalWrite(ledPin, LOW); // LED aus
delay(1000); // 1 Sekunde warten
}Module 3: Push Button
Button: turn on both LEDs for 5 seconds
#define LED_RED 25
#define LED_GREEN 26
#define BUTTON_PIN 23
void setup()
{
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, LOW);
}
void loop()
{
if (digitalRead(BUTTON_PIN) == HIGH) // If the button is pressed ...
{
digitalWrite(LED_RED, HIGH); // turn on the red LED
digitalWrite(LED_GREEN, HIGH); // turn on the green LED
delay(5000); // leave on for 5 seconds
digitalWrite(LED_RED, LOW); // turn off the red LED
digitalWrite(LED_GREEN, LOW); // turn off the green LED
}
}Module 4: Buzzer
Buzzer: Turn the sound on and off alternately
int buzzer = 19;
void setup()
{
pinMode(buzzer, OUTPUT);
}
void loop()
{
digitalWrite(buzzer, HIGH); // Sound on
delay(1000); // Wait 1 second
digitalWrite(buzzer, LOW); // Sound off
delay(5000); // Wait 5 seconds
}Module 5: Potentiometer
Potentiometer: Adjust the brightness of the red LED
#define LED_RED 25
#define POT_PIN 39 // Potentiometer on ADC 39
void setup()
{
pinMode(LED_RED, OUTPUT);
}
void loop()
{
// The analog value is stored in the variable v.
// The readings range from 0 to 4095.
int v = analogRead(POT_PIN);
// The map command converts values from 0 to 4095 into values from 0 to 255.
int duty = map(v, 0, 4095, 0, 255);
// A value between 0 and 255 is output on the red LED pin.
// Depending on the position of the potentiometer, it shines brighter or dimmer.
analogWrite(LED_RED, duty);
}Module 6: Ultrasonic distance measurement
Ultrasonic: Distance warning with the buzzer
#define TRIG_PIN 18
#define ECHO_PIN 17
#define BUZZER 19
void setup()
{
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER, OUTPUT);
digitalWrite(BUZZER, LOW);
}
void loop()
{
// Messimpuls senden
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Echo messen: Zeit, bis die Schallwelle zurückgeworfen wurde
long messzeit = pulseIn(ECHO_PIN, HIGH);
long cm = messzeit / 58;
// Buzzer steuern
if (cm < 20)
{
digitalWrite(BUZZER, HIGH);
delay(150);
digitalWrite(BUZZER, LOW);
delay(150);
}
else
{
digitalWrite(BUZZER, LOW);
delay(50);
}
}Module 7: OLED display
OLED display: Show text
#include <Wire.h>
#include <SSD1306Ascii.h>
#include <SSD1306AsciiWire.h>
SSD1306AsciiWire oled;
void setup()
{
Wire.begin(); // ESP32: SDA = 21, SCL = 22
oled.begin(&Adafruit128x64, 0x3C); // Start display (address 0x3C)
oled.setFont(System5x7); // simple font
oled.clear();
oled.setCursor(0, 0);
oled.println("Funduino"); // Output text
}
void loop()
{
// nothing else needed
}Module 8: Fan
Fan: on for 5 seconds, off for 5 seconds
#define FAN_PIN 4
void setup()
{
pinMode(FAN_PIN, OUTPUT);
}
void loop()
{
digitalWrite(FAN_PIN, HIGH); // ON
delay(5000);
digitalWrite(FAN_PIN, LOW); // OFF
delay(5000);
}Module 9: Infrared Remote Control
Infrared: Reading remote control button codes
#include <IRremote.hpp>
#define IR_PIN 16 // VS1838 signal on GPIO 16
void setup()
{
Serial.begin(115200);
delay(200);
IrReceiver.begin(IR_PIN, ENABLE_LED_FEEDBACK); // Start IR reception
Serial.println("IR-Ready: Press a button on the remote control.");
}
void loop()
{
if (IrReceiver.decode())
{
// Print a brief summary and the raw code in HEX
IrReceiver.printIRResultShort(&Serial);
Serial.print(" Raw: 0x");
Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);
// Get ready for the next signal
IrReceiver.resume();
}
}Module 10: WS2812 LED Bar
WS2812: eight pixels in eight colors
#include <Adafruit_NeoPixel.h>
#define WS_PIN 15
#define NUM_LEDS 8
Adafruit_NeoPixel strip(NUM_LEDS, WS_PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
strip.begin();
strip.setBrightness(80); // optional
}
void loop()
{
strip.setPixelColor(0, strip.Color(255, 0, 0)); // Red
strip.setPixelColor(1, strip.Color( 0, 255, 0)); // Green
strip.setPixelColor(2, strip.Color( 0, 0, 255)); // Blue
strip.setPixelColor(3, strip.Color(255, 255, 0)); // Yellow
strip.setPixelColor(4, strip.Color( 0, 255, 255)); // Cyan
strip.setPixelColor(5, strip.Color(255, 0, 255)); // Magenta
strip.setPixelColor(6, strip.Color(255, 255, 255)); // White
strip.setPixelColor(7, strip.Color(255, 128, 0)); // Orange
strip.show(); // Display colors
}Module 11: Temperature and Humidity with the DHT11
DHT11: Temperature and Humidity in the Serial Monitor
#include <DHT.h>
#define DHTPIN 14 // DHT data pin on the ESP32
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(115200);
dht.begin();
Serial.println("DHT started. Reading temperature and humidity ...");
}
void loop()
{
float hum = dht.readHumidity();
float temp = dht.readTemperature(); // in degrees Celsius
if (isnan(hum) || isnan(temp))
{
Serial.println("Error reading from DHT!");
}
else
{
Serial.print("Temp: ");
Serial.print(temp, 1); // 1 means one decimal place
Serial.print(" °C Humidity: ");
Serial.print(hum, 1);
Serial.println(" %");
}
delay(2000); // The DHT11 provides new readings at most about every 2 seconds.
}DHT11: Display readings on the OLED display
#include <Wire.h>
#include <SSD1306Ascii.h>
#include <SSD1306AsciiWire.h>
#include <DHT.h>
#define DHTPIN 14
#define DHTTYPE DHT11 // change to DHT22 if needed
SSD1306AsciiWire oled;
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Wire.begin(); // ESP32: SDA = 21, SCL = 22
oled.begin(&Adafruit128x64, 0x3C); // OLED address 0x3C
oled.setFont(System5x7);
oled.clear();
oled.println("DHT -> OLED");
dht.begin();
}
void loop()
{
float hum = dht.readHumidity();
float temp = dht.readTemperature(); // in degrees Celsius
oled.clear();
oled.setCursor(0, 0);
if (isnan(hum) || isnan(temp))
{
oled.println("Error reading!");
}
else
{
oled.println("Funduino");
oled.setCursor(0, 2);
oled.print("Temp: ");
oled.print(temp, 1);
oled.println(" C");
oled.setCursor(0, 3);
oled.print("Humidity: ");
oled.print(hum, 1);
oled.println(" %");
}
delay(2000);
}Module 12: Light intensity with the LDR
LDR: Measure brightness
#define LDR_PIN 35 // Pin 35 am ESP32
void setup()
{
Serial.begin(115200);
}
void loop()
{
// Unter raw wird der rohe Messwert als Zahl zwischen 0 und 4095 gespeichert.
int raw = analogRead(LDR_PIN);
// Der map-Befehl wandelt den Wert in einen prozentualen Wert um.
int pct = map(raw, 0, 4095, 0, 100);
Serial.print("LDR raw: ");
Serial.print(raw);
Serial.print(" Helligkeit: ");
Serial.print(pct);
Serial.println(" %");
delay(200);
}Module 13: PIR Motion Sensor SR602
Motion sensor: red LED when motion is detected
#define PIR_PIN 13
#define LED_RED 25
void setup()
{
pinMode(PIR_PIN, INPUT); // Der SR602 liefert ein aktives HIGH.
pinMode(LED_RED, OUTPUT);
digitalWrite(LED_RED, LOW);
}
void loop()
{
int motion = digitalRead(PIR_PIN);
if (motion == HIGH)
{
digitalWrite(LED_RED, HIGH); // Alarm: Bewegung erkannt
}
else
{
digitalWrite(LED_RED, LOW); // keine Bewegung
}
}Module 14: Servo and automatic door
Servo: Move to positions
#include <ESP32Servo.h>
#define SERVO_PIN 12
Servo servo;
void setup()
{
servo.attach(SERVO_PIN);
}
void loop()
{
servo.write(0); delay(1000); // 0 degrees
servo.write(90); delay(1000); // 90 degrees
servo.write(180); delay(1000); // 180 degrees
servo.write(90); delay(1000); // back to 90 degrees
}Module 15: MQ135 Gas Sensor
MQ135: Gas reading in the serial monitor
#define MQ135_PIN 34
int messwert = 0;
int prozent = 0;
void setup()
{
Serial.begin(115200);
delay(200);
Serial.println("MQ135 Start - bitte einige Minuten aufwaermen lassen");
}
void loop()
{
messwert = analogRead(MQ135_PIN);
// grobe Skalierung auf 0 bis 100 Prozent - nur als Tendenz!
prozent = map(messwert, 0, 4095, 0, 100);
Serial.print("MQ135 messwert: ");
Serial.print(messwert);
Serial.print(" Tendenz: ");
Serial.print(prozent);
Serial.println(" %");
delay(500);
}Module 16: Raindrop Sensor
Raindrop sensor: measure moisture and control LEDs
#define Tropfen_PIN 36 // Drop sensor, analog
#define LED_ROT 25
#define LED_GRUEN 26
void setup()
{
Serial.begin(115200);
pinMode(LED_ROT, OUTPUT);
pinMode(LED_GRUEN, OUTPUT);
}
void loop()
{
int raw = analogRead(Tropfen_PIN); // 0 to 4095
int Prozent = map(raw, 0, 4095, 0, 100); // rough moisture in percent
// Control LEDs
if (raw > 100) // threshold
{
digitalWrite(LED_ROT, HIGH);
digitalWrite(LED_GRUEN, LOW);
}
else
{
digitalWrite(LED_ROT, LOW);
digitalWrite(LED_GRUEN, HIGH);
}
// Output
Serial.print("RAW: ");
Serial.print(raw);
Serial.print(" Moisture in percent: ");
Serial.print(Prozent);
Serial.println(" %");
delay(200);
}Integration into Home Assistant
Rate the instructions