- L’objectiu d’avui és construir i programar un circuit fent servir Arduino que detecti la temperatura ambiental i que, segons sigui més alta o més baixa, faci encendre un led verd, taronja o vermell.
- Hem dissenyat el circuit a l’entorn TinkerCAD, un emulador online que permet comprovar el funcionament dels projectes.

- Per poder construir aquest projecte hem utilitzat varis components, els quals podeu observar a l’imatge.

- Comencem a introduir el codi. Primer, programarem la part encarregada d’importar la llibreria necessària per tractar amb la pantalla LCD i inicialitzar les variables globals que farà servir el programa:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);//Initialize the LCD library with their respective configuration
int sensor = A0; // A0-> LM35
int sensorValue = 0;
float temperature =0;
int redPin = 6; // Digital pin 6 -> red led
int greenPin = 7; // Digital pin 7-> green led
int yellowPin = 8; // Digital pin 8 -> yellow led
int speakerPin = 9; // Digital pin 9-> speaker
- Seguidament, a la funció setup, inicialitzem el nombre de files i columnes de la pantalla LCD i assignem funcions als pins de la placa Arduino:
void setup() {
lcd.begin (16,2); // Configure the LCD screen for the number of columns and rows (16×2)
delay (1000);
// Configure digital ports 6,7,8 and 10 as outputs make sure the LED’s are off
pinMode(greenPin, OUTPUT);
digitalWrite(greenPin, LOW);
pinMode(redPin, OUTPUT);
digitalWrite(redPin, LOW);
pinMode(yellowPin, OUTPUT);
digitalWrite(yellowPin, LOW);
pinMode(speakerPin, OUTPUT);
}
- Continuarem amb la funció loop, establint que la placa Arduino capturi la temperatura del sensor i canviï l’estat dels actuadors que té disponibles segons els valors llegits:
void loop() {
// Read de the value of the temperature sensor and proceednto scale it to obtenin the temperatura in Celsius degrees.
// according to the sensor manufacturer’s instructions
sensorValue= analogRead (sensor);
temperature= (sensorValue*(500.0/1023.0))- 50.0;
// Clean the LCD screen, configure the column a row where to display the value and
// configure the sound to be emitted by the buzzer
lcd.clear();
lcd.setCursor(0,0);
lcd.print(temperature);
lcd.print(“‘C”);
if(temperature<20){
// if the above conditions are not met, show the LOW temperature message and turn on the yellow LED (turning off the rest)
// and no soun will be emitted by the buzzer
lcd.setCursor (0,1);
lcd.print(“low temperature”);
digitalWrite(yellowPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(redPin, LOW);
playTone(600,500);
delay (100);
}
else if ( temperature>30) {
// if the above conditions are not met, show the HIGH temperature message and turn on the red LED (turning off the rest)
// and no soun will be emitted by the buzzer
lcd.setCursor (0,1);
lcd.print(“High temperature”);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(redPin, HIGH);
playTone(500,600);
delay (100);
playTone(500,800);
delay (100);
}
else{
// if the above conditions are not met, show the normal temperature message and turn on the green LED (turning off the rest)
// and no soun will be emitted by the buzzer
lcd.setCursor (0,1);
lcd.print(“Normal temperature”);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(redPin, LOW);
playTone(0,0);
delay (100);
}
}
- Per acabar, definirem una nova funció, encarregada de fer sonar el brunzidor:
// Function for emitting alarm tones at different duration (in millis) and frequency (in Hertz)
void playTone(long duration, int frequency){
duration *= 1000; // For each cycle of the fuction we multiply the duration variable
int period =(1.0/frequency)*1000000;// period or pulse width of output signals
long timeSpan =0; // Variable for the time span to define the duraton of the tone
while(timeSpan<duration){
digitalWrite(speakerPin,HIGH);
delayMicroseconds(period/2);
digitalWrite(speakerPin,LOW);
delayMicroseconds(period/2);
timeSpan += (period); // the time span increases for each period
}
}
- Si cliqueu l’enllaç, podreu veure el funcionament del circuit:
- https://drive.google.com/file/d/1hnGsEsYTuW3-UkvnDXgcK6lWnuuo_y5Y/view?usp=sharing
Deixa un comentari