analogRead range

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
lhag87
Posts: 2
Joined: 02 Apr 2023 22:32

analogRead range

Post by lhag87 »

Hi everbody,

I need to read an analog value with the Z-uno powered by 5V pin ( USB for testing ).

I've read [https://forum.z-wave.me/viewtopic.php?t=33688]

So I did a first try ( Z-Uno powered by 5.1V on 5V pin and 3V3 pin giving 3.17V )
with 1.58 V on A2 I get a value of 304
with 3.17V on A2 I get a value of 630

I'm surprised because it looks like the upper ref is the voltage on the 5V pin ( 304/1023*5.1 => 1.52 )
I thought it would be the voltage of 3V3 pin !

Setting 3V3 voltage on A0 and using analogReference( EXTERNAL ) analogRead give 1023 !

Code: Select all

    int intPower = analogRead( A2 ) ;
    Serial.print("power VCC REF: ");Serial.print(intPower);Serial.println();
    ...
    analogReference( EXTERNAL );
    intPower = analogRead( A2 ) ;
    Serial.print("power EXT REF: ");Serial.print(intPower);Serial.println();    

Code: Select all

          FIRMWARE          
          	 VERSION:		03.10
          	 BUILD_SEQUENCE:	00001842
          	 BUILD_DATETIME:	2023-01-17T16:38:12(MSK)
          	 SUPPORTED_HWREV:	0704   

Can someone explain ?
Thank's a lot
lhag87
Posts: 2
Joined: 02 Apr 2023 22:32

Re: analogRead range

Post by lhag87 »

Hi,

Searching in the files, I found in Z-Uno2/hardware/zw_cm4f/3.0.10/cores/includes/ZUNO_Definitions.h

Code: Select all

// ANALOG Refs
#define DEFAULT                                         adcRef5V
#define INTERNAL                                        adcRef1V25
#define INTERNAL2V56                                    adcRef2V5
#define INTERNAL5V                                      adcRef5V
#define EXTERNAL                                        adcRefExtSingle
So, yes the default reference is on 5V and we also find the 1.2V reference of the doc but there is also one 2.5V reference 8-)
and it's working !

Code: Select all

    analogReference( DEFAULT ) ;
    int intPower = analogRead( A2 ) ;
    Serial.print("power VCC REF: ");Serial.print(intPower);Serial.println();
    analogReference( INTERNAL2V56 );
    intPower = analogRead( A2 ) ;
    Serial.print("powerINTERNAL2V56 : ");Serial.print(intPower);Serial.println();
    analogReference( INTERNAL );
    intPower = analogRead( A2 ) ;
    Serial.print("powerINTERNAL : ");Serial.print(intPower);Serial.println(); 
with 0.5V on A2 it gives

Code: Select all

22:05:31.939 -> power VCC REF: 112
22:05:31.939 -> powerINTERNAL2V56 : 224
22:05:31.939 -> powerINTERNAL : 447
But I still don't understand why using analogReference( EXTERNAL) gives 1023 with 1.58V on A2 ( mesuring pin )
and ~3.1V ( value of 3V3 pin ) on A0 ( reference pin ) instead of something like ~510

Can someone explain ? where is make the mistake ?

Thank's
p0lyg0n1
Posts: 242
Joined: 04 Aug 2016 07:14

Re: analogRead range

Post by p0lyg0n1 »

lhag87 wrote:
02 Apr 2023 23:35
Hi everbody,

I need to read an analog value with the Z-uno powered by 5V pin ( USB for testing ).

Can someone explain ?
Thank's a lot
Hi,
If we explain it very briefly and schematically, then the reference is taken in 5V, but the signal is cut by protective diodes. As the upper limit, it is correct to take 5V, no more than 3.3 you will never get because of the protective diodes. Here is a piece of code adapted from ZWCCBattery.c, which explains how to correctly calculate the voltage with a default reference of 5V.

Code: Select all

uint32_t res = analogRead(A2);
res *= 5000; // Reference in millivolts
res >>= 10; // 10 bit resolution is default for ADC (backward compatibility with 1st gen.)
Serial.print("Voltage:");
Serial.print(res);
Serial.println(" mV");
By the way, if you replace pin A2 with BATTERY, you can measure the supply voltage of the ZUno itself. This is what the 5V reference is usually used for.
Regarding the external reference, I think that since the first releases, you are the first one to test it. It looks like there was something gone in the settings at some point. The default external input is differential. Thank you, we will test it.

Best regards,
Alex.
Post Reply