Uno: Convert 10bits to 8bits from analogRead()

Hello,

Long story short: I am trying to read a voltage value (0-5VDC) on an analog pin (A0) on Arduino Uno Board and output that voltage value to some drivers through an 8 bits number.

After looking around, it seems like the analogReadResolution (8 ) would be able to help but only on Due or Zero Board. Since I try to keep the hardware to be the same, I wonder if anyone has done some converting before on the Uno or has any hint or clue that can help solve the problem?

I appreciate all suggestions.

Thanks,

Since I try to keep the hardware to be the same, I wonder if anyone has done some converting before on the Uno or has any hint or clue that can help solve the problem?

int tenBitValue = analogRead(somePin);
byte eightBitValue = tenBitValue / 4;

You could even add two before the divide to get rounding.

besides a linear mapping, non linear mapping are possible

e.g

int x = pow(analogRead(A0)/1023.0, 3) * 256;

int x = sqrt(analogRead(A0)/1023.0) * 256;

int x = log(1 + 9 * analogRead(A0) /1023.0) * 256;

analogRead(A0)/1023.0 ==> maps all analog values to [0..1]

find a formula that outputs a range of [0..1]

then just multiply to the wanted integer range.

Thanks for all the quick responses and suggestions.

Since my signal doesn't have to be super accurate, I think the simple trick helped solve the problem by just dividing the 10-bit number to 4 in order to obtain the 8-bit number.

Yep, by far the best solution. You can also shift the value 2 places to the right and drop the last two digits.

Don't add the 2 to compensate for rounding! That only gives you an offset here. In a 8bit ADC (with 5V ref) the value 0b11111111 will be from the voltage 4.98046875V. And that's the same voltage on a 10bit ADC (again with 5V ref) when it outputs 0b1111111100. So just drop the last two bytes bits :wink: (divide it by 4 or shift by 2, doesn't matter).

septillion:
Yep, by far the best solution. You can also shift the value 2 places to the right and drop the last two digits.

Don't add the 2 to compensate for rounding! That only gives you an offset here. In a 8bit ADC (with 5V ref) the value 0b11111111 will be from the voltage 4.98046875V. And that's the same voltage on a 10bit ADC (again with 5V ref) when it outputs 0b1111111100. So just drop the last two bytes :wink: (divide it by 4 or shift by 2, doesn't matter).

Or even just drop the last two bits :wink: