Converting analog to digital

Can I just use the map() function as shown here?

var1 = analogRead(A0);
var2 = analogRead(A1);
map1 = map(var1, 0, 1023, 0, 1);
map2 = map(var2, 0, 1023, 0, 1);
digitalWrite(outpin1, map1);
digitalWrite(outpin2, map2);

I can post the full code for what I want to do if you need.

you can but you'll get 0 if the read was between 0 and 1022 and 1 for 1023 (due to the way the map function works)

➜ is that what you want ?

may be map1 = (var1 >= 512) ? 1 : 0; would be closer to your intent

another way is to use digitalRead(A0)and you'll get LOW (0) if the voltage is below a certain threshold and HIGH (1) otherwise.

the doc states:


When a pin is configured as an INPUT with pinMode(), and read with digitalRead(), the Arduino (ATmega) will report LOW if:

  • a voltage less than 1.5V is present at the pin (5V boards)
  • a voltage less than 1.0V (Approx) is present at the pin (3.3V boards)


When a pin is configured as an INPUT with pinMode(), and read with digitalRead(), the Arduino (ATmega) will report HIGH if:

  • a voltage greater than 3.0V is present at the pin (5V boards)
  • a voltage greater than 2.0V is present at the pin (3.3V boards)

➜ so in between you take your chance for HIGH or LOW

So for the in between bit there's just a floating input? That seems useless.

Could be written

map1 = map (var1, 0, 1024, 0, 2));

But it is an expensive way of doing

map1 = (var1 > 511) ? 1 : 0;

No, there is just no guarantee on where the threshold is and thus if you'll get a LOW or a HIGH for sure.

you should get something coherent ie it won't read LOW once and HIGH the next time for the exact same value if the power supply did not change

So basically I'll just have to experiment.

I've not seen this before. Can you explain the syntax to me?

Sorry. I'm more adept with the wiring and building side of things than the programming.

The ternary operator

Or you could simply use A0 as a digital pin

@J-M-L talked about this but it seems unreliable.

Search for ternary operator. See e.g. ?: - Wikipedia

I'm not sure why you're trying to what you're trying to do. with few exceptions, analog inputs can be used as digital inputs so digitalWrite will work

So it's just a weird way of doing if, then, else. That makes sense.

Basically what you are saying is that what I'm suggesting will work, but that this is a better way to program it. Is that right?


No more than any of the other pins.

1 Like

I will try both of these solutions. Thank you!

My understanding is that it is reliable but not fully predictable in the sense that you can't guarantee that something between 1.5 and 3V will lead to the same HIGH or LOW on two different boards or that the same input will lead to the same HIGH or LOW if you power supply has somewhat varied between the two reads. But that's just a guess, I've not studied in details how the PORT registers are set electrically

Not weird, it's part of the language, so something good to know :slight_smile:

as I said, what you are suggesting will lead to 0 and 1 but it's going to be mostly 0 and only 1 if the input value is read at 1023.

so technically work, but does it match your needs/expectations is the real question.

➜ the expression where you compare the analogRead value lets you set the threshold under which it's 0 and above which it's 1.
➜ the digitalRead() will give you 0 and 1 but you don't get to set the exact threshold

I think I'll go with the analogRead/map thing because this is for a combat robot, so it needs to have pretty precise control.

I'll repeat myself but you understand that if you use map() you'll get 0 most of the time and depending on how good the input is, you might never get exactly 1023 and thus never get 1 ?

I would go for the comparison then you have way more control (and it's probably faster than map)

const int threshold1 = 512;
const int threshold2 = 448;

digitalWrite(outpin1, (analogRead(A0) > threshold1) ? HIGH : LOW);
digitalWrite(outpin2, (analogRead(A1) > threshold2) ? HIGH : LOW);

you get to have different threshold if needed (depending on your joystick or whatever is connected which might be biased )

That's what I meant by:

the analogRead/map thing

Thank you for your help!

What kind of signal is applied to the pin? If it is really an analogue signal, you should include a hyteresis. Otherwise, your digital signal will become unstable when the external voltage equals the threshold voltage.
With digitalRead, there is an internal Schmitt trigger that provides hyteresis.