Analog controlling digital output

Hey,
I want to use my Arduino Uno to output a digital signal when a certain threshold is reached by the analog pin A0. I am not sure why my code is not working, all that is happening is that the circuit is turned on the whole time regardless if the threshold is met.

Here is my code

//This program will output a 5V signal to the 555 timer when the voltage output from the temp sensor reaches .93V

void setup() {
  // sets the pins as output

pinMode(6,OUTPUT);

}
//the loop will run continuously while the Arduino is on, after setup has run once.
void loop() {
  int sensor_value= analogRead(A0);
  if(sensor_value>=191);  //.93V
    digitalWrite(6,HIGH);
 
      }

Please edit your post, select all code and click image ; next save your post.

Regarding your code.
Once your sensor value has reach the threshold, pin 6 will be made HIGH. This will be remembered till you explicitely make pin 6 LOW (what you did not do).

I have added sensor value in which pin 6 will be Low if A0 is below the threshold, but I am still running into the same problem where the program only seems to have one mode of operation.

//This program will output a 5V signal to the 555 timer when the voltage output from the temp sensor reaches .93V

void setup() {
  // sets the pins as output

pinMode(6,OUTPUT);

}
//the loop will run continuously while the Arduino is on, after setup has run once.
void loop() {
  int sensor_value= analogRead(A0);
  if(sensor_value>=191);  // greater than .93V
    digitalWrite(6,HIGH);
 if(sensor_value<=190); //less than .93V
    digitalWrite(6,LOW);
      }

remove the ; after the if(…) statments.

1 Like

AH! thank you for your help!

Things are easier to read and understand if you name your constants and let the compiler do the math rather than using magic numbers.

// This program will output a 5V signal to the 555 timer
// when the voltage output from the temp sensor reaches
// 0.93V

const float arefVoltage = 5.0;
const float thresholdVoltage = 0.93;
const int threshold = (thresholdVoltage * 1024.0) / arefVoltage;

const byte InputVoltagePin = A0;
const byte OutputPin = 6;

void setup()
{
  // sets the pins as output
  pinMode(OutputPin, OUTPUT);
}

//the loop will run continuously while the Arduino is on, after setup has run once.
void loop()
{
  // OutputPin = HIGH if voltage above threshold, else LOW
  digitalWrite(OutputPin, analogRead(InputVoltagePin) >= threshold);
}

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