[SOLVED] Trouble using Analog Inputs with WiringPi

Post Reply
nametable7
Posts: 5
Joined: Sun Mar 20, 2016 10:18 am
languages_spoken: English
ODROIDs: ODROID C2
Has thanked: 0
Been thanked: 0
Contact:

[SOLVED] Trouble using Analog Inputs with WiringPi

Unread post by nametable7 » Mon Mar 21, 2016 1:19 pm

I am having trouble getting the values from input pins using WiringPi(https://github.com/hardkernel/wiringPi). In an article for the C1 I found that I could see the analog input values in the terminal by using the method below. http://odroid.com/dokuwiki/doku.php?id= ... rdware_adc

Code: Select all

odroid@odroid64:/sys/class/saradc$ cat ch0
896
odroid@odroid64:/sys/class/saradc$ cat ch0
873
odroid@odroid64:/sys/class/saradc$ cat ch0
978
odroid@odroid64:/sys/class/saradc$ cat ch0
980
I am not sure how I could use that in a C program to obtain the analog values but I know that WiringPi should be able to give me access to them.

Analog input zero is pin 29 in WiringPi, but there is no value shown using "gpio readall" or using "gpio aread 29"

Code: Select all

odroid@odroid64:/sys/class/saradc$ gpio readall
 +------+-----+----------+------+ Model  ODROID-C2 +------+----------+-----+------+
 | GPIO | wPi |   Name   | Mode | V | Physical | V | Mode |   Name   | wPi | GPIO |
 +------+-----+----------+------+---+----++----+---+------+----------+-----+------+
 |      |     |     3.3v |      |   |  1 || 2  |   |      | 5v       |     |      |
 |      |   8 |    SDA.1 |      |   |  3 || 4  |   |      | 5V       |     |      |
 |      |   9 |    SCL.1 |      |   |  5 || 6  |   |      | 0v       |     |      |
 |  249 |   7 | GPIO.249 |   IN | 1 |  7 || 8  |   |      | TxD1     | 15  |      |
 |      |     |       0v |      |   |  9 || 10 |   |      | RxD1     | 16  |      |
 |  247 |   0 | GPIO.247 |  OUT | 0 | 11 || 12 | 0 | OUT  | GPIO.238 | 1   |  238 |
 |  239 |   2 | GPIO.239 |   IN | 1 | 13 || 14 |   |      | 0v       |     |      |
 |  237 |   3 | GPIO.237 |   IN | 1 | 15 || 16 | 1 | IN   | GPIO.236 | 4   |  236 |
 |      |     |     3.3v |      |   | 17 || 18 | 1 | IN   | GPIO.233 | 5   |  233 |
 |  235 |  12 | GPIO.235 |   IN | 1 | 19 || 20 |   |      | 0v       |     |      |
 |  232 |  13 | GPIO.232 |   IN | 1 | 21 || 22 | 1 | IN   | GPIO.231 | 6   |  231 |
 |  230 |  14 | GPIO.230 |   IN | 1 | 23 || 24 | 1 | IN   | GPIO.229 | 10  |  229 |
 |      |     |       0v |      |   | 25 || 26 | 1 | IN   | GPIO.225 | 11  |  225 |
 |      |  30 |    SDA.2 |      |   | 27 || 28 |   |      | SCL.2    | 31  |      |
 |  228 |  21 | GPIO.228 |   IN | 1 | 29 || 30 |   |      | 0v       |     |      |
 |  219 |  22 | GPIO.219 |   IN | 1 | 31 || 32 | 1 | IN   | GPIO.224 | 26  |  224 |
 |  234 |  23 | GPIO.234 |   IN | 0 | 33 || 34 |   |      | 0v       |     |      |
 |  214 |  24 | GPIO.214 |   IN | 1 | 35 || 36 | 1 | IN   | GPIO.218 | 27  |  218 |
 |      |  25 |    AIN.1 |      |   | 37 || 38 |   |      | 1v8      | 28  |      |
 |      |     |       0v |      |   | 39 || 40 |   |      | AIN.0    | 29  |      | <--- this input
 +------+-----+----------+------+---+----++----+---+------+----------+-----+------+

odroid@odroid64:/sys/class/saradc$ gpio aread 29
0
The "analogRead(29)" within my C program also produces a zero.

I am not sure if I am reading the analog value wrong or if I have some configuration settings that I need to change. Any help would be appreciated. :?: :D

User avatar
odroid
Site Admin
Posts: 33014
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Has thanked: 289 times
Been thanked: 412 times
Contact:

Re: Trouble using Analog Inputs with WiringPi

Unread post by odroid » Mon Mar 21, 2016 1:38 pm

You can open the ADC node "/sys/class/saradc/ch0" with fopen() function.
And read the value with fread() function.

We will check the feasibility of adding the ADC API in the WiringPi.

nametable7
Posts: 5
Joined: Sun Mar 20, 2016 10:18 am
languages_spoken: English
ODROIDs: ODROID C2
Has thanked: 0
Been thanked: 0
Contact:

Re: Trouble using Analog Inputs with WiringPi

Unread post by nametable7 » Mon Mar 21, 2016 1:46 pm

Thank you. I will try the fopen() and fread() functions. :D

User avatar
odroid
Site Admin
Posts: 33014
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Has thanked: 289 times
Been thanked: 412 times
Contact:

Re: Trouble using Analog Inputs with WiringPi

Unread post by odroid » Mon Mar 21, 2016 5:44 pm

Try this much simpler way. It worked well.

Code: Select all

#define PORT_ADC1 0  // Pin #40
#define PORT_ADC2 1  // Pin #37
// ADC read out
adcValue1 = analogRead (PORT_ADC1);
adcValue2 = analogRead (PORT_ADC2);

nametable7
Posts: 5
Joined: Sun Mar 20, 2016 10:18 am
languages_spoken: English
ODROIDs: ODROID C2
Has thanked: 0
Been thanked: 0
Contact:

Re: Trouble using Analog Inputs with WiringPi

Unread post by nametable7 » Tue Mar 22, 2016 9:10 am

Thank you. I didn't realize that the pin values for the analog inputs start back at zero again when using analogRead() . Works great. :)

Post Reply

Return to “Issues”

Who is online

Users browsing this forum: No registered users and 1 guest