i am currently dealing with a small project. I am electronic student and i am trying to do a system where i am using a fan, relay, arduino uno, LM35 and battery of 9V. I did the next programming code but when i connect the pc with the microcontroller, Arduino read the temperature values but the fan does not move.
Someone could help me please
#define SENS_PIN A0
#define FAN_PIN 5
int Vin;
float Temperatura;
float TF;
void setup() {
pinMode(FAN_PIN,OUTPUT);
Serial.begin(9600);
}
void loop() {
Vin=analogRead(SENS_PIN);
Temperatura=(500*Vin)/1023*(1.8)+32;
TF=Temperatura;
Serial.print("Temperatura: ");
Serial.print(TF);
Serial.println("fahrenheit");
if(TF>71){
digitalWrite(FAN_PIN,HIGH);
}
else if(TF<71){
digitalWrite(FAN_PIN,LOW);
}
delay(1000);
}
Antes de nada, decirte que estas escribiendo en un Foro de Idioma Español, no Ingles. Aun así te he resuelto el problema que tenias.
No declaraste 1 entrada, y declaraste mal los pines. Ademas de indicar un "else" de una manera innecesaria...
Aquí tienes el código 100% funcional y testado.
#include <Arduino.h>
const int SensorPin = A0; //No habias declarado como constantes la entrada
const int FanPin = 5; //No habias declarado como constantes la salida
int LecturaSensor;
float Temperatura;
void setup() {
Serial.begin(9600);
pinMode(SensorPin, INPUT); //No habias declarado la entrada del Sensor
pinMode(FanPin, OUTPUT);
}
void loop() {
LecturaSensor = analogRead(SensorPin);
Temperatura = (500*LecturaSensor)/1023*(1.8)+32;
Serial.print("Temperatura: ");
Serial.print(Temperatura);
Serial.println("fahrenheit");
if (Temperatura >= 71) {
digitalWrite(FanPin, HIGH);
}
else { //Hiciste una mala configuracion aqui
digitalWrite(FanPin, LOW);
}
delay(1000);
}
Surbyte
September 19, 2019, 2:55pm
3
Read How to use this forum and next time post in the English forum if you write in English , this is the Spanish forum.
Moved!
Check that your battery can run the fan if you connect to it directly. If that really is a 9V smoke detector battery, it may not be able to.