2CE705/2IT705 Internet of Things
PRACTICAL –4_Solution
Aim: - Arduino architecture and basic programming.
Serial.begin()
Serial.end()
Serialread()
Serialwrite()
Serial.print()
Serial.println()
Serial.available()
Experiment
1. Increase and decrease the brightness of LED using potentiometer and
display the voltage level on serial monitor
Code:
const int analogInPin = A0; // pin connected to the photoresistor
const int analogOutPin = 9; //pin connected to the LED
int sensorValue = 0; // Value read by the photoresistor
int outputValue = 0; // Value sent to the LED
void setup() {
Serial.begin(9600);
pinMode(analogOutPin, OUTPUT);
pinMode(analogInPin, INPUT);
}
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
}
CE/IT Department
2CE705/2IT705 Internet of Things
2. Read the current temperature of room and display it on serial monitor
Code:
float tmp;
#define PIN A0
void setup()
{
Serial.begin(9600);
pinMode(PIN,INPUT);
}
void loop()
{
tmp = analogRead(PIN);
tmp = tmp * 5000/1024;
tmp = tmp/10;
tmp = tmp - 50;
Serial.print("Temperature=");
Serial.println(tmp);
}
3. Read the current temperature of room and ON the RGB Led with specific
colour according to current temperature value
Code:
float tmp;
#define PIN A0
#define R 1
#define G 3
#define B 5
void setup()
{
Serial.begin(9600);
pinMode(PIN,INPUT);
pinMode(R,OUTPUT);
pinMode(G,OUTPUT);
pinMode(B,OUTPUT);
}
void loop()
{
tmp = analogRead(PIN);
tmp = tmp * 5000/1024;
tmp = tmp/10;
tmp = tmp - 50;
Serial.print("Temperature=");
Serial.println(tmp);
if(tmp>50)//if temperature above of 25 degrees
CE/IT Department
2CE705/2IT705 Internet of Things
{
digitalWrite(R,HIGH);
digitalWrite(G,LOW);
digitalWrite(B,LOW);
Serial.println("RED");
}
else if(tmp>=0&&tmp<=50)//if temperature is under 23 degrees
{
digitalWrite(R,LOW);
digitalWrite(G,HIGH);
digitalWrite(B,LOW);
Serial.println("GREEN");
}
else
{
digitalWrite(R,LOW);
digitalWrite(G,LOW);
digitalWrite(B,HIGH);
Serial.println("BLUE");
}
}
CE/IT Department