🌿 Greenhouse Part 2: Measuring Temperature & Light Intensity

Hi lovely people! 👋 In the last article we talked about the soil moisture sensor. In this article, we will take a look at how to measure temperature using DS18b20 and measure light intensity using a photocell.

Step 1: Wiring up the Circuit

We will be using:

  • Arduino UNO
  • Breadboard
  • DS18B20 Temperature Sensor
  • Photocell (any typical one)
  • 4.7KΩ resistor
  • 10KΩ resistor
  • Jumper Wires

Wire up the circuit like this:

Temp Light Sensor Note: Click on the image to view a larger version

I have wired up the 4.7KΩ resistor in parallel with the positive terminal and the digital input wire coming out of DS18B20. The 10KΩ resistor is connected between the photocell and the 5V input.

The reading of the photocell increases with a decrease in brightness and decreases with an increase in brightness. It ranges from 0 to 1023.

The DS18B20 temp sensor gives readings in degree Celsius by default. You can convert it into Fahrenheit if you want though.

Step 2: Coding the Arduino

You need to install the Dallas Temperature library and the One Wire library for this code to work. You can download them using the library manager in the Arduino IDE.

// Include the libraries
#include <DallasTemperature.h>
#include <OneWire.h> 

// This is where the temp sensor is plugged in. 
// Change this to the analog input where your temp
// sensor is plugged in (if different)
#define ONE_WIRE_BUS 2 
OneWire oneWire(ONE_WIRE_BUS); 

int photoPin = A1;

// Pass the oneWire ref to Dallas Temperature Sensor
DallasTemperature sensors(&oneWire);

void setup(){
    // Set the photocell sensor pin to INPUT mode
    pinMode(photoPin, INPUT);
    // Setup the temp sensor and serial comm

void loop() {
    // Get photocell reading
    int photoValue = analogRead(photoPin);
    Serial.print("Photocell reading: ");

    // Get temperature reading
    Serial.print(" - Temperature: "); 

    // We input 0 as index because we can have more than 
    // 1 IC attached to the same bus
    int tempVal = sensors.getTempCByIndex(0);


The code is straightforward. We include the libraries we will be using. We define analog input 2 as ONE_WIRE_BUS and tell DallasTemperature that the sensor is attached on input 2.

In setup we simply set pin modes and setup temperature sensor and serial communication.

In loop, we take readings from photocell and DS18B20 and send them over serial to the laptop.

Now save the code and upload it to Arduino. Open the serial monitor and you should start seeing readings like this:

Temp Light Sensor Readings

If everything is working then perfect! If not, go through the wiring steps again. If it still doesn’t make any sense go through the references I have linked below.

I will see you in the next article where we wire up an LCD with all of this! ❤️