How to Build a Simple Greenhouse Environmental Monitor With Arduino

by Fekry in Circuits > Arduino

105 Views, 2 Favorites, 0 Comments

How to Build a Simple Greenhouse Environmental Monitor With Arduino

example.png

Maintaining optimal environmental conditions is crucial for healthy plant growth in a greenhouse. In this Instructable, I'll guide you through building a Greenhouse Monitor that connects to an Arduino device to provide real-time monitoring of temperature and light intensity using a TMP36 temperature sensor and a photoresistor. We'll also create a simple web application to display the data, allowing you to track conditions conveniently from your computer. All the code and resources are available on GitHub. If you find this project helpful, please consider favoriting this Instructable and starring the GitHub repository. Your support helps more makers discover this resource! Let's get started!

Supplies

Materials:

  1. Arduino Uno (or compatible board)
  2. Breadboard
  3. Jumper Wires
  4. TMP36 Temperature Sensor
  5. Photoresistor
  6. 10 kΩ Resistor (for the photoresistor voltage divider)
  7. USB Cable (for Arduino connection)

Tools:

  1. Computer with Arduino IDE installed
  2. Internet Browser (Google Chrome or Microsoft Edge recommended)
  3. Measuring Tools (optional, for precise assembly)

Understand the Circuit Diagram

IMG_5217.jpg

Before assembling the hardware, let's understand how the components are connected.

  1. TMP36 Temperature Sensor:
  2. Pin 1 (Vs): Connect to +5V on the Arduino.
  3. Pin 2 (Vout): Connect to Analog Pin A0 on the Arduino.
  4. Pin 3 (GND): Connect to Ground (GND) on the Arduino.
  5. Photoresistor with Voltage Divider:
  6. Connect one end of the photoresistor to +5V.
  7. Connect the other end of the photoresistor to Analog Pin A1 and one leg of the 10 kΩ resistor.
  8. Connect the other leg of the 10 kΩ resistor to Ground (GND).

Note: The voltage divider allows the Arduino to read varying voltage levels from the photoresistor based on light intensity.

Assemble the Circuit on the Breadboard

CircuitDiagram.png
  1. Place the TMP36 Sensor:
  2. Insert the TMP36 sensor into the breadboard with the flat face forward.
  3. Left Pin (Vs): Connect to the +5V rail.
  4. Middle Pin (Vout): Connect to Analog Pin A0.
  5. Right Pin (GND): Connect to the Ground rail.
  6. Set Up the Photoresistor Circuit:
  7. Connect one end of the photoresistor to the +5V rail.
  8. Connect the other end to a point on the breadboard.
  9. From that point, connect a jumper wire to Analog Pin A1.
  10. Also from that point, connect the 10 kΩ resistor to the Ground rail.
  11. Connect Power Rails:
  12. Use jumper wires to connect the +5V and GND pins from the Arduino to the respective rails on the breadboard.
  13. Double-Check Connections:
  14. Ensure all components are connected correctly to prevent any short circuits.

Visual Aid: Refer to the circuit diagram to verify your setup.

Install the Arduino IDE and Connect the Arduino

Screenshot 2024-11-13 at 1.40.32 AM.png
  1. Download and Install Arduino IDE:
  2. Visit the Arduino Software page and download the IDE for your operating system.
  3. Follow the installation instructions provided.
  4. Connect the Arduino to Your Computer:
  5. Use the USB cable to connect the Arduino to your computer.
  6. The power LED on the Arduino should light up.
  7. Select the Correct Board and Port:
  8. Open the Arduino IDE.
  9. Go to Tools > Board and select your Arduino model.
  10. Go to Tools > Port and select the port corresponding to your Arduino (e.g., COM3 on Windows or /dev/cu.usbmodem on macOS).

Write and Upload the Arduino Code

Screenshot 2024-11-13 at 1.43.17 AM.png

We'll write code to read data from the sensors and send it over serial communication.

Arduino Code:

// Define sensor pins
#define TEMP_SENSOR_PIN A0
#define PHOTOCELL_PIN A1

void setup() {
// Initialize Serial communication at 9600 baud rate
Serial.begin(9600);
}

void loop() {
// Read temperature sensor (TMP36)
int tempReading = analogRead(TEMP_SENSOR_PIN);
float tempVoltage = tempReading * (5.0 / 1023.0); // Convert to voltage (0 - 5V)
float temperatureC = (tempVoltage - 0.5) * 100.0; // Convert voltage to Celsius

// Read photocell sensor
int lightReading = analogRead(PHOTOCELL_PIN);
// Optionally, map the lightReading to a percentage
int lightPercentage = map(lightReading, 0, 1023, 0, 100);

// Create JSON formatted string
String dataString = "{";
dataString += "\"temperature\":" + String(temperatureC, 2) + ",";
dataString += "\"light\":" + String(lightPercentage);
dataString += "}";

// Send data over Serial
Serial.println(dataString);

// Wait for 1 second before next reading
delay(1000);
}
  1. Enter the Code:
  2. Copy and paste the code into the Arduino IDE.
  3. Upload the Code:
  4. Click the Upload button (right arrow icon) to compile and upload the code to the Arduino.
  5. Ensure there are no error messages.
  6. Test Serial Output:
  7. Open the Serial Monitor from Tools > Serial Monitor.
  8. You should see temperature and light intensity readings in JSON format.

Set Up and Run the Web Application

Screenshot 2024-11-13 at 12.17.17 AM.png

I've developed a simple web application to display the sensor data. All the necessary files are available on our GitHub repository.

  1. Download the Web App Files:
  2. Clone or download the repository:
git clone https://github.com/FekryMostafa/Arduino-Greenhouse-Monitor.git
  1. Alternatively, download the ZIP file and extract it to a folder on your computer.
  2. Navigate to the Project Folder:
  3. Open the folder containing index.html, app.js, and other resources.
  4. Open the HTML File:
  5. Locate index.html in your project folder.
  6. Right-click on the file and select "Open with" and choose Google Chrome or Microsoft Edge.
  7. Enable Serial Communication in Browser:
  8. Ensure you're using a browser that supports the Web Serial API (e.g., Chrome or Edge).
  9. Allow Access to Serial Ports:
  10. The first time you use the Web Serial API, your browser may prompt you to allow access to connected serial devices. Accept the prompt to proceed.

Connect the Web App to the Arduino

Screenshot 2024-11-13 at 12.20.09 AM.png
example.png
  1. Connect the Arduino:
  2. Ensure the Arduino is connected to your computer and running the uploaded code.
  3. Use the Web App:
  4. In the web app, click the "Connect to Arduino" button.
  5. A prompt will appear to select a serial port.
  6. Select the Correct Serial Port:
  7. On macOS, look for a port named something like /dev/cu.usbmodemXXXX.
  8. On Windows, it might be COM3, COM4, etc.
  9. Select the port and click Connect.
  10. View Real-Time Data:
  11. The web app should start displaying temperature and light intensity readings.
  12. Temperature will be shown in degrees Celsius with an option to switch to Fahrenheit.
  13. Light intensity is displayed as a percentage.
  14. Toggle Light/Dark Mode:
  15. Use the provided switch to change between light and dark themes for the web app.

Reflecting on the Project

Building the Greenhouse Monitor was an insightful experience that combined electronics, programming, and web development. Here's what I learned:

  1. Arduino Serial Communication: Understanding how to read sensor data and transmit it over serial communication is fundamental for IoT projects.
  2. Web Serial API: Using the Web Serial API allows direct communication between a web browser and hardware devices, opening up new possibilities for web-based interfaces.
  3. Sensor Integration: Working with the TMP36 temperature sensor and a photoresistor provided practical experience in analog sensor data acquisition and voltage dividers.
  4. Cross-Platform Compatibility: Recognizing differences in serial port naming between macOS and Windows is essential for user-friendly instructions.
  5. User Interface Design: Implementing light and dark modes and temperature unit toggling enhanced the usability of the web app.

This project not only serves as a functional tool for monitoring greenhouse conditions but also demonstrates how hardware and software can be integrated to create interactive applications.