Help with reading and responding to Analog readings

Hello,
im new to Arduino (especially the code bit) so be prepared for a stupid question...

i am trying to write code so that when the Arduino detects a signal (5v) on the analog pin (A0 to be exact), it responds by outputting 5v from one of the digital pins (2). This is just to get the concept working, and then i can expand it so lots of things happen when the board senses a 5v signal on one of the pins. (Could really be any, i am using Analog atm).

The Code so far:

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);




void setup() {
lcd.begin(16, 2);
pinMode(A0, INPUT);
pinMode(2, OUTPUT);
Serial.begin(9600);
}

void loop() {
   int signal_pin = A0;
   int output_pin = 2;
Serial.println(analogRead(signal_pin));
if(signal_pin>=900)
digitalWrite(output_pin, HIGH);
else
digitalWrite(output_pin, LOW);
delay(10);

}

This is probably totally wrong, pls correct me...
( i have tried loads of different combinations in the code, swapping alias names for pin numbers etc... Also, if you are wondering what all that about LCD is, i have an LCD connected to the Arduino that i am using, however right now this is not relevant or doing anything, just trying to get the above mentioned concept working.)

Thanks

Have you tried it ?

Looks like it should work

signal_pin will never be greater than 900, it will always be A0.

try this way:

void setup() {
     //lcd.begin(16, 2);
     pinMode(A0, INPUT);
     pinMode(2, OUTPUT);
     Serial.begin(9600);
}

void loop() {
     int signal_pin = A0;
     int output_pin = 2;
     if(analogRead(signal_pin)>=900)
         digitalWrite(output_pin, HIGH);
     else
        digitalWrite(output_pin, LOW);
     delay(10);

}

But learn to use ctrl-T to autoformat:

void setup() {
  //lcd.begin(16, 2);
  pinMode(A0, INPUT);
  pinMode(2, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int signal_pin = A0;
  int output_pin = 2;
  if (analogRead(signal_pin) >= 900)
    digitalWrite(output_pin, HIGH);
  else
    digitalWrite(output_pin, LOW);
  delay(10);
}

Also it's better to declare pin identifiers globally, and as 'const'.

const int signal_pin = A0;
const int output_pin = 2;

void setup() {
  //lcd.begin(16, 2);
  pinMode(A0, INPUT);
  pinMode(2, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (analogRead(signal_pin) >= 900)
    digitalWrite(output_pin, HIGH);
  else
    digitalWrite(output_pin, LOW);
  delay(10);
}

You also failed to use the identifiers for pin config:

  pinMode(A0, INPUT);
  pinMode(2, OUTPUT);

In fact, this was a consequence of declaring them in the wrong place.

just tried what ruilviana suggested, it worked fine. Cheers!
Hopefully now i can build off this...

Also, using the serial monitor, there seems to be a lot of noise, also if you touch the A0 input it activates the digital output, is there a way to reduce this with resistors or something?

You're almost never going to get exactly 900. :wink: Usually with anything analog you should use less-than or greater-than, or both if you want to check for a range of values.

With 5V you'd expect to read 1023 but voltages can vary and the ADC isn't always "perfect" so you might read something a little lower.

Just FYI - With digital signals there is also a "range" of voltages... The Aruduino reads anything greater than 3V as high and anything less than 1.5V as low. In-between is undefined and it might read high or low.

Hint - For troubleshooting & debugging, it's helpful to "print-out" variables (or other little messages) to the serial monitor (or to the LCD once you get it working). This allows you to "see" the analog value, etc., and as your program gets more complicated you can add little messages like, "Reading analog input", again to "see" what your program is doing, especially when it's not behaving as expected.

1 Like

I'm sure you mean, "build on this".

Well, what the heck do you have it connected to now? :smile: Or plan to? Because "what you would use" depends heavily on that.

aarg,
i don't have it connected to anything new, what i mean, is i have a jumper wire in the A0 header pin thing, and if you touch that with your bare finger, the reading can get very high, (900+), according to the serial monitor.
This is not a huge issue, however (relevant or not), can someone please explain what pull up resistors are on the board? i have heard of them, but have no clue...

Well, it turns out i have figured out the problem with all the noise and false alarms, using a ground connection, a 1M ohm resistor, however it would be helpful if someone could still explain the pull up resistor thing, just for future knowledge. :grinning:

Sure, what pull up resistor thing? You only referred to, "the Arduino that I'm using" so we don't know which one. But none that I know of have any on board resistors on the analog inputs. You can enable internal pull up resistors on analog inputs on the AVR at least, usually not a good idea.

I looked back through the thread and couldn't find any mention of pull up resistors...

Also you neglected to explain what you are planning to connect this to.