dturgut
Posts: 7
Joined: Thu Sep 14, 2017 1:54 am

Unable to read Pi input

Thu Sep 14, 2017 2:08 am

Hi everyone,

So I am trying to read in the state of a PWM MA3 absolute rotary encoder with the Raspberry Pi. When I run the code below, only values of zero are being printed. When I run the Python equivalent code, I get a series of ones and zeros, so I'm not sure what I am doing wrong with the C code.

Code: Select all

#include <wiringPi.h>
#include <time.h>
#include <stdio.h>

#define Pin 1

int main(void)
{
       pinMode(Pin, INPUT);
       int pos[100000];
       int i = 0;
       while(i<100000)
       {
            printf("%u", digitalRead(Pin));
            ++i;
        }
        return 0;
}
To build, I simply execute:
gcc -o test test.c
./test

All help will be appreciated.

1dot0
Posts: 385
Joined: Mon Nov 28, 2016 12:31 pm

Re: Unable to read Pi input

Thu Sep 14, 2017 7:45 am

I don't know your encoder and don't understand your code, but
- where do you setup wiringPi? By which pin numbering scheme?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 4544
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Unable to read Pi input

Thu Sep 14, 2017 11:27 am

https://projects.drogon.net/raspberry-pi/wiringpi/pins/ for the reference on what WiringPi considers to be GPIO 1.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
gordon@drogon.net
Posts: 1970
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Unable to read Pi input

Thu Sep 14, 2017 1:15 pm

dturgut wrote:
Thu Sep 14, 2017 2:08 am
Hi everyone,

So I am trying to read in the state of a PWM MA3 absolute rotary encoder with the Raspberry Pi. When I run the code below, only values of zero are being printed. When I run the Python equivalent code, I get a series of ones and zeros, so I'm not sure what I am doing wrong with the C code.

Code: Select all

#include <wiringPi.h>
#include <time.h>
#include <stdio.h>

#define Pin 1

int main(void)
{
       pinMode(Pin, INPUT);
       int pos[100000];
       int i = 0;
       while(i<100000)
       {
            printf("%u", digitalRead(Pin));
            ++i;
        }
        return 0;
}
To build, I simply execute:
gcc -o test test.c
./test

All help will be appreciated.
You're missing a call to wiringPiSetup() before the first pinMode().

incidentally, test stuff like this in a shell/terminal before you even write code with: e.g.

Code: Select all

gpio mode 1 in
while true; do echo -n `gpio read 1`; done
-Gordon
--
Gordons projects: https://projects.drogon.net/

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 12 guests