0% found this document useful (0 votes)
25 views4 pages

Practical 9

Iot with AI

Uploaded by

nilesh gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

Practical 9

Iot with AI

Uploaded by

nilesh gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

Aim: Write a program read the temperature sensor and send the values to the serial

monitor on the computer

const int tmp36Pin = A0; // Analog pin where TMP36 is connected

void setup() {

Serial.begin(9600); // Start serial communication at 9600 baud

void loop() {

int sensorValue = analogRead(tmp36Pin); // Read the analog value from TMP36

float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog value to voltage

float temperatureC = (voltage - 0.5) * 100; // Convert voltage to temperature in Celsius

// Print the temperature to the serial monitor

Serial.print("Temperature: ");

Serial.print(temperatureC);

Serial.println(" °C");

delay(1000); // Wait for 1 second before taking the next reading

}
Here’s a detailed line-by-line explanation of the Arduino code for reading temperature from the TMP36
sensor:

cpp

Copy code

const int tmp36Pin = A0; // Analog pin where TMP36 is connected

const int tmp36Pin = A0;: Defines a constant integer named tmp36Pin and assigns it the value A0. This is
the analog pin on the Arduino where the TMP36 sensor’s output is connected. Using const ensures that
this pin number cannot be changed accidentally later in the code.

cpp

Copy code

void setup() {

Serial.begin(9600); // Start serial communication at 9600 baud

void setup(): The setup function is called once when the Arduino starts or resets. It is used to initialize
settings.

Serial.begin(9600);: Initializes serial communication at a baud rate of 9600 bits per second (bps). This
allows the Arduino to send data to the Serial Monitor on your computer.

cpp

Copy code

void loop() {

int sensorValue = analogRead(tmp36Pin); // Read the analog value from TMP36

float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog value to voltage

float temperatureC = (voltage - 0.5) * 100; // Convert voltage to temperature in Celsius

// Print the temperature to the serial monitor

Serial.print("Temperature: ");
Serial.print(temperatureC);

Serial.println(" °C");

delay(1000); // Wait for 1 second before taking the next reading

void loop(): The loop function runs repeatedly after the setup function has finished. This is where you
place code that needs to run continuously.

int sensorValue = analogRead(tmp36Pin);: Reads the analog value from the pin defined by tmp36Pin
(A0). The analogRead() function converts the voltage on the pin (from 0V to 5V) into a value between 0
and 1023 (10-bit ADC resolution).

float voltage = sensorValue * (5.0 / 1023.0);: Converts the analog reading into a voltage. The Arduino’s
ADC converts the 10-bit value (0-1023) into a voltage between 0V and 5V. This formula converts the
integer reading into a corresponding voltage:

5.0 is the reference voltage (5V for most Arduino boards).

1023.0 is the maximum ADC value (2^10 - 1 for 10-bit resolution).

float temperatureC = (voltage - 0.5) * 100;: Converts the voltage to temperature in Celsius. The TMP36
sensor outputs 500mV (0.5V) at 0°C, and the output increases by 10mV per °C:

voltage - 0.5 adjusts for the 500mV offset.

Multiplying by 100 converts the voltage to temperature (since 10mV/°C = 100mV/°C per 1°C).

Serial.print("Temperature: ");: Sends the string "Temperature: " to the Serial Monitor.

Serial.print(temperatureC);: Sends the temperature value to the Serial Monitor.

Serial.println(" °C");: Sends " °C" to the Serial Monitor and moves to the next line.
delay(1000);: Pauses the program for 1000 milliseconds (1 second) before repeating the loop. This
reduces the frequency of readings and updates in the Serial Monitor.

Summary

Setup: Initializes serial communication.

Loop: Reads analog values, converts them to temperature, prints the result, and then pauses for 1
second before repeating.

This code will continuously read the temperature from the TMP36 sensor and display it on the Serial
Monitor in Celsius.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy