Why is my input always high

I have a temperature control project in which I have a fan that is automatically turned on or off depending on the reading given from a sensor. And that part works fine but now I need a manual override toggle switch which when it is closed/on it will turn the fan on until manually switched off. at which point it will resume the normal program. However the switch is constantly reading as high and therefore stopping the rest of the function regardless of the switches position. aside from this, will the code work as intended once this issue is fixed or am I missing something?

edit: my switch pin is connected 1 to pin 7 and 3 to 5V.

  lcd.begin(16, 2);
  Serial.begin(9600); 
  
  pinMode(fan,OUTPUT);
  pinMode(switch_pin, INPUT);
  
  delay(500);
 }//Delay to let system boot


void loop() {
  
if
  (digitalRead(switch_pin) == HIGH)
  {digitalWrite(fan,LOW);
  lcd.setCursor(0,1);
  lcd.print("Manual Stop");
  Serial.print("Manual Stop");}

if
  (digitalRead(switch_pin) == LOW){
  DHT.read11(dht_apin);
  Serial.print(" Temperature = ");
  Serial.print(DHT.temperature); 
  Serial.println("C  ");    
  delay(1500);//Wait 1.5 seconds before accessing sensor again.
   
  lcd.setCursor(0,0);
  lcd.print(DHT.temperature);
  lcd.println(" *C");
  
  if
    (isnan(DHT.temperature)) {
    Serial.println(" Failed to read transformer temperature");
    lcd.print("Failed reading");}

  if
    (DHT.temperature>=40) 
    {digitalWrite(fan,HIGH);
    lcd.setCursor(0,1);
    lcd.print("Fan = ON");
    Serial.print("Fan = ON");}

  else
    {digitalWrite(fan,LOW);
    lcd.setCursor(0,1);
    lcd.print("Auto Fan = OFF");
    Serial.print("Auto Fan = OFF");}
    }

 }

TEMP_SENSOR_PROJECT.ino (1.31 KB)

Change:
pinMode(switch_pin, INPUT);
To:
pinMode(switch_pin, INPUT_PULLUP);


Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0


Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu. [code]Paste your sketch here[/code]


FYI

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How have you got your switch wired?
Do you have a pull up or pull down resistor on the digital input that your switch is connected?
Do you have a DMM?

Thanks.. Tom.. :slight_smile:

Consistent positioning of { and } would be helpful.
You only read temperature conditionally, but test temperature anyway - is that intentional?

This is the simplest way for me personally. Apologies if this is difficult to understand. this is how everything is connected excluding earths.
And as for the wiring most of it isn't visible so the schematic is the best thing to follow.

Configure the input with INPUT_PULLUP as suggested in reply #1, and then run pin 3 of the toggle switch to ground instead of +5V. The polarity of the switch will now be opposite, it will be LOW when pressed. But that can always be accommodated in the program logic.

TheMemberFormerlyKnownAsAWOL:
Consistent positioning of { and } would be helpful.

void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);

  pinMode(fan, OUTPUT);
  pinMode(switch_pin, INPUT);

  delay(500);
}//Delay to let system boot


void loop()
{
  if (digitalRead(switch_pin) == HIGH)
  {
    digitalWrite(fan, LOW);
    lcd.setCursor(0, 1);
    lcd.print("Manual Stop");
    Serial.print("Manual Stop");
  }

  if (digitalRead(switch_pin) == LOW)
  {
    DHT.read11(dht_apin);
    Serial.print(" Temperature = ");
    Serial.print(DHT.temperature);
    Serial.println("C  ");
    delay(1500);//Wait 1.5 seconds before accessing sensor again.

    lcd.setCursor(0, 0);
    lcd.print(DHT.temperature);
    lcd.println(" *C");
   }

    if (isnan(DHT.temperature))
    {
      Serial.println(" Failed to read transformer temperature");
      lcd.print("Failed reading");
    }

    if (DHT.temperature >= 40)
    { 
      digitalWrite(fan, HIGH);
      lcd.setCursor(0, 1);
      lcd.print("Fan = ON");
      Serial.print("Fan = ON");
    }

    else
    { 
      digitalWrite(fan, LOW);
      lcd.setCursor(0, 1);
      lcd.print("Auto Fan = OFF");
      Serial.print("Auto Fan = OFF");
    }
}

So, I've changed my code to INPUT_PULLUP as well as swapped pin 3 to GND instead of 5V. none of which has made a difference. And yes, the temperature needs to be read consistently.

Please post your code as it is now

TomGeorge:
Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How have you got your switch wired?
Do you have a pull up or pull down resistor on the digital input that your switch is connected?
Do you have a DMM?

Thanks.. Tom.. :slight_smile:

I was told on a separate forum not to use a resistor and I don't know what a DMM is?

UKHeliBob:
Please post your code as it is now

here is my code now.

#include <LiquidCrystal.h>
#include <dht.h>

#define dht_apin A0
dht DHT;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
int fan = 6;
int switch_pin = 7;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {

lcd.begin(16, 2);
Serial.begin(9600);

pinMode(fan,OUTPUT);
pinMode(switch_pin,INPUT_PULLUP);

delay(500);
}//Delay to let system boot

void loop() {

if
(digitalRead(switch_pin) == HIGH)
{digitalWrite(fan,LOW);
lcd.setCursor(0,1);
lcd.print("Manual Stop");
Serial.print("Manual Stop");}

if
(digitalRead(switch_pin) == LOW){
DHT.read11(dht_apin);
Serial.print(" Temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
delay(1500);//Wait 1.5 seconds before accessing sensor again.

lcd.setCursor(0,0);
lcd.print(DHT.temperature);
lcd.println(" *C");

if
(isnan(DHT.temperature)) {
Serial.println(" Failed to read transformer temperature");
lcd.print("Failed reading");}

if
(DHT.temperature>=40)
{digitalWrite(fan,HIGH);
lcd.setCursor(0,1);
lcd.print("Fan = ON");
Serial.print("Fan = ON");}

else
{digitalWrite(fan,LOW);
lcd.setCursor(0,1);
lcd.print("Auto Fan = OFF");
Serial.print("Auto Fan = OFF");}
}

}

You need a pullup or pulldown resistor on the switch if you don't use a pinMode() of INPUT_PULLUP to activate the built in resistor

DMM - Digital MultiMeter

Ah yes I have a DMM and my program is now using the pullup but I'm still not getting anywhere with it.

but I'm still not getting anywhere with it.

What a waste of words.

Tell us what the code IS doing

When you changed to input pullup did you:

  • wire the switch from the pin to ground, and
  • change the code logic so that switch open is high, closed is low?

tina220800:
Ah yes I have a DMM and my program is now using the pullup but I'm still not getting anywhere with it.

Post your code

Hi,
I think you have a problem here, is it a drawing error or do you have the fan ON continuously?
relays111.jpg
In fact check your fan wiring as you have it connected to live and the UNO gnd!!!!!! not the mains Neutral.

Tom.... :slight_smile:

relays111.jpg

Hi,
Can I suggest you get a pencil and paper out and hand draw your circuit.
Reverse engineer your project so you will be drawing EXACTLY what you have.

Thanks.. Tom.. :slight_smile:

UKHeliBob:
Post your code

I already posted my code in reply to your other comment. but here it is again.

#include <LiquidCrystal.h>
#include <dht.h>

#define dht_apin A0
dht DHT;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
int fan = 6;
int switch_pin = 7;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {

lcd.begin(16, 2);
Serial.begin(9600);

pinMode(fan,OUTPUT);
pinMode(switch_pin,INPUT_PULLUP);

delay(500);
}//Delay to let system boot

void loop() {

if
(digitalRead(switch_pin) == HIGH)
{digitalWrite(fan,LOW);
lcd.setCursor(0,1);
lcd.print("Manual Stop");
Serial.print("Manual Stop");}

if
(digitalRead(switch_pin) == LOW){
DHT.read11(dht_apin);
Serial.print(" Temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
delay(1500);//Wait 1.5 seconds before accessing sensor again.

lcd.setCursor(0,0);
lcd.print(DHT.temperature);
lcd.println(" *C");

if
(isnan(DHT.temperature)) {
Serial.println(" Failed to read transformer temperature");
lcd.print("Failed reading");}

if
(DHT.temperature>=40)
{digitalWrite(fan,HIGH);
lcd.setCursor(0,1);
lcd.print("Fan = ON");
Serial.print("Fan = ON");}

else
{digitalWrite(fan,LOW);
lcd.setCursor(0,1);
lcd.print("Auto Fan = OFF");
Serial.print("Auto Fan = OFF");}
}

}

blomcrestlight:
When you changed to input pullup did you:

  • wire the switch from the pin to ground, and
  • change the code logic so that switch open is high, closed is low?

yes my pin is now to ground. And I have done it both ways with no difference, it still reads "manual stop".

TheMemberFormerlyKnownAsAWOL:
What a waste of words.

Tell us what the code IS doing

My code is still doing exactly what i said in my first message. The switch is only reading high and therefore the LCD is only "manual stop". Whether i flick the switch up or down it's only reading one way and seems it is stopping the rest of my program from running.

Hi,

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
**http://forum.arduino.cc/index.php/topic,148850.0.html. **
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Tom... :slight_smile:

I already posted my code in reply to your other comment

But you have changed it since then

  if (digitalRead(switch_pin) == HIGH)
  {
    digitalWrite(fan, LOW);
    lcd.setCursor(0, 1);
    lcd.print("Manual Stop");
    Serial.print("Manual Stop");
  }

As you are now using INPUT_PULLUP the state of the pin will be HIGH when the switch is not closed. Have you now got the switch wired to take the pin LOW when the switch is closed ?