Lesson 27: Smart greenhouse
A greenhouse that looks after itself
- This is the last project: one Arduino controls a whole greenhouse.
- It reads two sensors (temperature and light) and drives three actuators (a window servo, a grow light and a heater).
- You put each sensor reading into its own function, so
loopreads like a list of rules.
Words for this lesson
| Word | Meaning |
|---|---|
| greenhouse | a glass house where plants grow |
| grow light | a lamp that gives plants light at night |
| heater | a machine that makes the air warmer |
| return | send a value back from a function |
| bool | a value that is true or false |
The rules
| Sensor | Rule | Actuator |
|---|---|---|
| temperature (A0) | above 28 °C | window open (servo 90°) |
| temperature (A0) | below 15 °C | heater on (pin 5) |
| light (A1) | below 300 (dark) | grow light on (pin 6) |
A function that answers yes or no
bool isBig(int n) {
return n > 100;
}
void setup() {
Serial.begin(9600);
Serial.println(isBig(250));
Serial.println(isBig(40));
}
void loop() {
}
isBig(250)returnstrueand prints 1;isBig(40)returnsfalseand prints 0.
In the exam
A greenhouse is the most common automated system question in IGCSE Computer Science (0478). A full answer:
- Sensors (temperature, light, sometimes humidity) send readings to the microprocessor; an ADC turns the analogue readings into numbers.
- The microprocessor compares each reading with stored values (28 °C, 15 °C, the light level).
- If a reading is outside the range, it sends a signal to an actuator: a motor opens the window, the heater or the lights switch on.
- If the reading is in the range, nothing changes.
- This repeats all the time (
loop).
Dificultad 4/5 · 280 monedas + un pequeño bono en el primer intento de hoy
A greenhouse gets too hot in the sun. A temperature sensor is on A0 (°C = (volts − 0.5) × 100, as in lesson 19) and a servo opens the window on pin 9. Finish the function readTemp() so it returns the temperature. In loop: print Temp: and the temperature, open the window (90°) above 28 °C, and close it (0°) when it is not.
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 5/5 · 400 monedas + un pequeño bono en el primer intento de hoy
Now the whole greenhouse. A light sensor is on A1 (a small number means dark). Add a function bool isDark() that returns true when analogRead(A1) is below 300. Then in loop: the grow light on pin 6 is on when it is dark, and the heater on pin 5 is on below 15 °C. The window works as before.
Haz clic en Ejecutar para ver la salida aquí.