Connect the LM35 temperature sensor to an analog input of the ATmega328P. Calculate the ADC step and resolution, show the result on a virtual terminal and an LCD, and link the bit knowledge from the C course to the ADC registers.
Inside an electrical panel the temperature can exceed 50 °C in summer; drives and power supplies age early at that temperature. A simple fix: put a temperature sensor in the panel and run a ventilation fan when a threshold is crossed. In this lesson you will build the measurement part: the LM35 sensor, the microcontroller's ADC (analog-to-digital converter) and a virtual terminal showing the result. You already learned the transistor needed to drive the fan in lesson five.
Theory: the LM35 and the 10-bit ADC
The LM35 is an analog sensor that outputs 10 mV per degree Celsius. In the basic connection it gives 0 V at 0 °C, 0.25 V at 25 °C and 1.00 V at 100 °C. It has three pins: +Vs (supply), Vout (output) and GND.
The ATmega328P's ADC is 10-bit: it converts the input voltage into an integer between 0 and 1023. With a 5 V reference, one step is:
Step = Vref / 1024 = 5 / 1024 = 4.883 mV
Temperature resolution = 4.883 mV / (10 mV/C) = about 0.49 C
At 25 C: Vout = 0.250 V
ADC = 0.250 / 0.004883 = 51.2 -> 51 (the ADC rounds down)
Back-conversion: 51 x 5 / 1024 = 0.249 V -> 24.9 C
The bit logic you saw in the C course becomes concrete here: 10 bits means 2 to the power 10 = 1024 different values. More bits mean a smaller step and a finer measurement.
The sensor circuit in Proteus
- Add an ATMEGA328P as in the previous lesson and set its clock to 16 MHz.
- Press P, search for LM35 and add it. Connect +Vs to 5 V (a POWER terminal from terminals mode; you can label it +5V), GND to ground and Vout to PC0 (Arduino A0).
- Add a VIRTUAL TERMINAL from virtual instruments mode. Connect the terminal's RXD to the chip's PD1/TXD pin, not PD0/RXD: the terminal receives what the chip sends.
- Double-click the terminal and set the baud rate to 9600 (it must match the code).
- For a correct analog measurement, make sure the chip's analog supply and reference pins (AVCC, AREF) are connected. Proteus often ties them to hidden power nets; if they are visible in your version, connect AVCC to 5 V.
- Compile the code below, load the HEX and start the simulation. Change the temperature with the small buttons on the LM35 model (or in its properties, depending on the version).
// Temperature measurement with LM35, result to the serial port
const int SENSOR_PIN = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int adc = analogRead(SENSOR_PIN);
float volt = adc * 5.0 / 1024.0;
float temp = volt * 100.0; // 10 mV = 1 C
Serial.print("ADC: ");
Serial.print(adc);
Serial.print(" Sicaklik: ");
Serial.print(temp, 1);
Serial.println(" C");
delay(1000);
}
ADC: 51 Sicaklik: 24.9 C (sensor 25 C)
ADC: 61 Sicaklik: 29.8 C (sensor 30 C)
Depending on the model's output and the ADC rounding, the values may move by ±1 step. Note: 30 °C shows as 29.8 °C on screen. That is not a bug; it is the result of the 0.49 °C resolution.
Better resolution: the 1.1 V internal reference
At panel temperatures the LM35 output rarely goes above 1 V. With a 5 V reference you waste four fifths of the ADC range. The ATmega328P has an internal reference of about 1.1 V; on Arduino you select it with analogReference(INTERNAL):
Step = 1.1 / 1024 = 1.074 mV -> resolution about 0.11 C
Highest measurable: 1.1 V -> 110 C
At 25 C: ADC = 0.250 / 0.001074 = 232.7 -> 232
Temperature = 232 x 110 / 1024 = 24.9 C
On real chips the internal reference is not exactly 1.1 V; it varies by a few percent from part to part. For precise field measurements you calibrate at a known temperature.
At register level: a bridge to the C course
Behind the scenes, analogRead sets up the ADC registers. Writing the same thing in plain C is exactly where the bit shifting and masking from the C course pay off:
#include <avr/io.h>
#include <stdint.h>
void adc_baslat(void) {
// ADC on, prescaler 128: 16 MHz / 128 = 125 kHz
ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
}
uint16_t adc_oku(uint8_t kanal) {
ADMUX = (1 << REFS0) | (kanal & 0x07); // reference AVCC, select channel
ADCSRA |= (1 << ADSC); // start conversion
while (ADCSRA & (1 << ADSC)) { } // done when the bit clears
return ADC; // 10-bit result
}
(1 << ADSC) selects the relevant bit, |= sets that bit without touching the others, and & asks whether the bit is still set. The mask kanal & 0x07 keeps only the lower three bits of the channel number so it cannot disturb the reference bits.
Showing it on an LCD
There will be no computer in the panel, so showing the result on a 16x2 LCD is more realistic. In Proteus the character LCD model LM016L is widely used. If you declare LiquidCrystal lcd(12, 11, 5, 4, 3, 2); with Arduino's LiquidCrystal library, the wiring is: RS → PB4 (D12), E → PB3 (D11), D4 → PD5, D5 → PD4, D6 → PD3, D7 → PD2; RW → ground. You then use lcd.print instead of Serial.print.
Common mistakes
Integer division: adc * 5 / 1024 is computed with ints and gives 0 for every ADC value below 205. Use decimal constants (5.0, 1024.0).
Mixing up TX and RX: the chip's TXD pin goes to the terminal's RXD. If you connect both to TXD, the terminal stays empty.
Baud rate mismatch: if the code uses 9600 and the terminal another rate, you see garbage characters.
Exercise
Change the code to use the 1.1 V internal reference. When the temperature exceeds 35 °C, turn on the fan transistor connected to PB0 (D8); when it drops below 33 °C, turn it off (2 °C hysteresis). Try it in Proteus by changing the LM35 temperature.
Hint: the conversion factor is now 110.0 / 1024.0. For hysteresis, keep the fan's current state in a variable.
Solution
const int SENSOR_PIN = A0;
const int FAN_PIN = 8;
bool fanAcik = false;
void setup() {
analogReference(INTERNAL); // about 1.1 V
pinMode(FAN_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int adc = analogRead(SENSOR_PIN);
float temp = adc * 110.0 / 1024.0;
if (!fanAcik && temp > 35.0) fanAcik = true;
if (fanAcik && temp < 33.0) fanAcik = false;
digitalWrite(FAN_PIN, fanAcik ? HIGH : LOW);
Serial.print(temp, 1);
Serial.println(fanAcik ? " C FAN ACIK" : " C FAN KAPALI");
delay(1000);
}
24.9 C FAN KAPALI
36.0 C FAN ACIK
33.9 C FAN ACIK
31.9 C FAN KAPALI
Thanks to hysteresis, the fan doesn't keep switching on and off while the temperature hovers between 34 and 35 °C; relay and fan life gets longer. The output shows roughly what you see when you set the sensor to 25, 36, 34 and 32 °C in turn (assuming the internal reference is exactly 1.1 V). "FAN ACIK" means fan on, "FAN KAPALI" fan off.
In short: the path from sensor voltage to temperature goes through knowing the ADC step; a smaller reference gives better resolution. In the next and final lesson you will see how to turn this schematic into a printed circuit board.
// Spot the bug +10 XP
Exactly one line below is wrong. Click the faulty line.
All numbers on the right are integers, so the arithmetic is done with ints: 51 x 5 = 255, 255 / 1024 = 0, and the result is 0.00. Assigning to a float does not fix it. Correct: float temp = adc * 5.0 / 1024.0 * 100.0; This is the field version of the integer division trap from the C course.
// Mini quiz
Click an option; you will see right away, with the reason, whether it is correct or not.
Mark it when you finish: your progress is saved and you earn +20 XP.
Tip: ← → keys let you move between lessons.


