Measuring Temperature and Light Intensity with Arduino


In this article, we’ll explore how to Measuring Temperature and Light Intensity with Arduino, measure temperature using the DS18B20 sensor and measure light intensity using a photocell. Let’s dive in! So, let’s get started and dive into the world of temperature and light intensity measurements using DS18B20 and photocell sensors!

The DS18B20 sensor offers accurate and reliable temperature readings, making it an ideal choice for various applications such as weather monitoring, home automation, and industrial processes. On the other hand, the photocell, also known as a light-dependent resistor (LDR), can detect variations in light intensity, allowing us to design systems that respond to changes in ambient lighting conditions.

Hardware Required

You will require the following Hardware Components for Measuring Temperature and Light Intensity with Arduino.

Components#Buy From Amazon
Arduino UNO1Buy Now
DS18B20 Temperature Sensor1Buy Now
Photocell (any typical one)1Buy Now
9v DC Adapter (Optional)1Buy Now
Resistor 4.7KΩ1Buy Now
Resistor 10KΩ1Buy Now
Jumper WiresFewBuy Now
Breadboard1Buy Now

Circuit Explanation

  1. Power Supply: Connect the +5V and GND pins of the Arduino to the respective power rails on the breadboard to provide power to the components.
  2. DS18B20 Temperature Sensor: Connect the DS18B20 sensor to the Arduino. The DS18B20 has three pins: VCC (power), GND (ground), and DQ (data). Connect VCC to +5V, GND to GND, and DQ to digital pin D2 on the Arduino.
  3. Photocell (LDR): Connect one end of the photocell to +5V and the other end to analog input A0 on the Arduino. In between the photocell and A0, connect a 10KΩ resistor in series to create a voltage divider circuit.
  4. DS18B20 Resistor: Connect a 4.7KΩ resistor in parallel with the DS18B20 data pin (DQ) and the VCC pin. This pull-up resistor ensures proper communication with the sensor.

Circuit Diagram

The following circuit shows you the connection of the Measuring Temperature and Light Intensity with Arduino Please make the connection carefully


Working Explanation

  • The DS18B20 temperature sensor communicates with the Arduino using the One-Wire protocol. It measures the ambient temperature and sends the digital temperature data to the Arduino.
  • The photocell changes its resistance based on the intensity of light falling on it. The voltage across the photocell changes, and the Arduino reads this voltage as an analog input.
  • The Arduino reads the digital temperature data from the DS18B20 sensor using the One-Wire library and converts it into degrees Celsius.
  • Simultaneously, the Arduino reads the analog voltage from the photocell and converts it into a digital value using the analog-to-digital converter (ADC).
  • The Arduino can now display the temperature reading and the light intensity value on the Serial Monitor or perform further actions based on the sensor data.

Installing Arduino IDE Software

First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow

Installing Libraries

Now when you are Ready to upload the code, to the Arduino Board you will need first to add the Following Libraries in Arduino, If you Learn How to add the library in the Arduino step-by-step guide click on how to install the library Button given Blow


//For more Projects:

// 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 simple and starts by including the necessary libraries. We define analog input 2 as ONE_WIRE_BUS and inform DallasTemperature that the sensor is connected to input 2.

In the setup, we set pin modes, initialize the temperature sensor, and enable serial communication.

Within the loop, we continuously read values from the photocell and DS18B20, sending them via serial to the connected laptop.

Output Result

To use the code, save and upload it to the Arduino board. Open the serial monitor, and you will start seeing readings displayed in real-time.



  1. Home Automation: The combined use of DS18B20 and photocell enables smart home systems to regulate indoor temperature and lighting based on ambient conditions. It allows automated heating/cooling and smart lighting solutions, optimizing energy usage and enhancing comfort.
  2. Environmental Monitoring: Deploying this sensor combination in weather stations or environmental monitoring setups provides real-time data on temperature and light intensity, aiding in climate research, agriculture, and ecological studies.
  3. Greenhouse Management: Integrating DS18B20 and photocell in greenhouses helps control temperature and optimize natural light exposure for ideal plant growth, leading to increased yield and resource efficiency.
  4. Weather Balloons and UAVs: These sensors are used in meteorological balloons or unmanned aerial vehicles (UAVs) to measure atmospheric temperature and brightness, offering valuable data for weather forecasting and atmospheric research.
  5. Industrial Applications: The temperature and light intensity data are valuable in various industrial processes, such as HVAC systems, lighting control, and process automation, ensuring optimized working conditions and energy conservation

Leave a Comment