You will be building Arduino-based circuits with the help of a temperature sensor and a power relay.
In this experiment, you will write programs to make one LED blink, to make 8 LEDs turn on, one at a time, at different rates, and controlled by the status of a switch, and implement the guess the pattern game using two switches and 2 two-bit numbers.
#include <dht.h> // include DHT11 Library
dht DHT;
#define pintemp 8 // Signal pin of DHT11 connected to digital output 8
int pinOut = 7; // Relay signal pin connected to digital output 7
void setup(){
Serial.begin(9600);
pinMode(pinOut, OUTPUT);
pinMode(pintemp, INPUT);
}
void loop()
{
delay(5000);
int chk = DHT.read11(pintemp);
Serial.print("Temperature = ");
Serial.print(DHT.temperature);
Serial.print(" Humidity = ");
Serial.print(DHT.humidity);
if (DHT.temperature <= 25){
Serial.print(" status of pinOut= ");
Serial.println(LOW);
}
else {
Serial.print(" status of pinOut= ");
Serial.println(HIGH);
}
delay(100);
}
Execute the program given above, by adding one line to the if block and one line to the else block (note that the definition of the output pin for the relay is going to be used to control the LED), so that an LED will turn on when the temperature exceeds 25⁰C, and off otherwise. The LED will represent a fan that will be turned on through the relay controlling a power outlet when the temperature exceeds the threshold temperature. The instructors may bring the setup (relay, outlet and a fan or a load) for you to test the whole process, but the LED should be sufficient to validate it.
const int analogInPin = A0;
int sensorValue = 0;
void setup() {
// declare pin to be an output:
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
sensorValue = analogRead(analogInPin);
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\n");
delay(2);
if((sensorValue>=100)&&(sensorValue<=300)){
digitalWrite(2,HIGH);
delay(100);
}
else if((sensorValue>=301)&&(sensorValue<=350)){
digitalWrite(3,HIGH);
delay(100);
}
else if((sensorValue>=351)&&(sensorValue<=400)){
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
}
else{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
delay(1000);
}
}
Rewrite the program based on the following specifications:
You can assume that the 1/5 threshold is related to drought, and the 4/5 threshold is flood level. However, this sensor is not very practical as corrosion can lead to false readings after a long-time immersion in water.
// Define the pins for the ultrasonic sensor
const int trigPin = 9; // Trigger pin
const int echoPin = 10; // Echo pin
// Variables
float duration;
float distance; // Distance measured by the sensor
float containerHeight = 10; // Height of the container in inches
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Triggering the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reading the echo duration
duration = pulseIn(echoPin, HIGH);
// Calculating distance in inches
distance = duration * 0.0131 / 2.0; // Speed of sound = 0.0131 inches per microsecond
// Displaying distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" inches");
// Checking if water level is reached
if (distance <= containerHeight-8) {
Serial.println("Water level reached!");
// You can add additional actions here when the water level is reached
}
delay(1000); // Adjust delay as needed
Execute this program based on your own parameters (Height of container, distance when full, pins…) to display Water Level Reached when exceeding the full threshold and use also an LED to turn ON during that situation. Play with the angle of the ultrasound sensor to find the optimum angle for highest accuracy measurement. Bring two empty cups (one to carry water, and the other to contain the water as it is poured in for measurement purposes).
In your lab report, describe the most relevant parts of your source codes.