ESP8266 - A0 read switch

Hi

I have a switch connected to D1 + 10k resistor, this works

My next switch is connected to A0 (last available pin) +10k, this do not work.

Any pro tip to get?

My code for this issue:


int switchTare = D1;    // select the input pin for the switch
int switchTareValue = 0;  // variable to store the value coming from the sensor
const int switchAlarm = A0;
int switchAlarmValue = 0;  // variable to store the value coming from the sensor

Void loop()
1) This works
switchTareValue = analogRead(switchTare);     // read switch tare  S1


2) this work not
switchAlarmValue = analogRead(switchAlarm);   // read switch alarm (A0)


void loop()
if (switchTareValue > 0) {    /
    tareValue = 1;
    Serial.print("Tare valgt ");
    Serial.println(tareValue);
    delay(5000);      
    functionTare();        //call function tare
  }
  if (switchAlarmValue < 1) {    
    alarmValue = 1;
    Serial.print("ALARM!!!!  ");
    Serial.println(alarmValue);
    delay(5000);            /
    functionAlarm();        //call function alarm
  }
  else {
    switchAlarmValue = 0; 
  }


Functions
void functionTare() {
  scale.tare();
   tareValue = 0;   // set tare back to 0, tare function done
   Serial.print("Tare done, tareValue set to: ");
   Serial.println(tareValue);
}

// This function is also hardwired to a relay that can trigger an alarm/siren
void functionAlarm() {
   Serial.print("ALARM sent to Cayenne: ");
   Serial.println(alarmValue);
}



Thanks up front

Which analog GPIO pin does switchAlarm read when int switchAlarmValue = 0; is set to 0?

The ESP8266 only has one analog pin, A0.

hI

As far as I can see there is no GPIO defined fo A0

I assure you A0 is associated with a pin number.

Still the ESP8266 has only one analog pin. and this switchAlarmValue = analogRead(switchAlarm); // read switch alarm (A0) will not work because you assign the value of switchAlarm as 0 which is not the A0 pin of the ESP8266, which only has ONE A:D converter.

Are you trying to make an Analog input pin act like a digital pin?

Hi

I have made a test code, it reads correct values between 0 and 1024

What is trouble is that if sentence don't seems to work, i get ALARM in any case

Code:

void setup() {
  Serial.begin(57600);
}
void loop() {
  Serial.print("Value: ");
  Serial.println(analogRead(A0));
  Serial.println("--------------------------");

  if (analogRead(A0) < 1024); { 
    Serial.print("ALARM!!!!  ");
    delay(2000);            //litt pause for å se teksten på serial print
    //functionAlarm();        //call function alarm
  }
}

Serial output:

A0 low
image

A0 high
image

What do I wrong?

Not sure, but one thing to check is that you read the pin twice, both in the Serial.println(... and later in the if (analogRead(A0)...
Is there a chance the value might have changed in between? Why not read the pin only once to be sure.

Remove the ;

Tom... :smiley: :+1: :coffee: :australia:

Hi, thanks for answer

Im not in my workshop now, need to test later.

Something like this you suggests?

float value;

void setup() {
  Serial.begin(57600);
}
void loop() {
  Serial.print("Value: ");
  value = analogRead(A0);
  Serial.println(value);
  Serial.println("--------------------------");

  if (value < 1024); { 
    Serial.print("ALARM!!!!  ");
    delay(2000);            
    //functionAlarm();        //call function alarm
  }
}

[/quote]

Yes, but @TomGeorge found the most probable reason, i.e. the extra ;

No,
please read this below

if (analogRead(A0) < 1024); {

Remove the ;

if (analogRead(A0) < 1024 )     {

Tom.. :smiley: :+1: :coffee: :coffee: :coffee: :australia:

did you try printing out the A0 read to see what it is?

Serial.println( analogRead(A0) );//I bet most of the readings are below 1024?
if (analogRead(A0) < 1024); { 
    Serial.print("ALARM!!!!  ");
    delay(2000);            //litt pause for å se teksten på serial print
    //functionAlarm();        //call function alarm
  }

The important thing to understand here is the difference between > (greater than) and < (less than).

Works like a charm.

Devil is in the details!

Thanks @TomGeorge

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.