Reading GPIO state in beagle bone black

136 views
Skip to first unread message

Megha Bhirade

unread,
Jul 17, 2019, 12:52:50 AM7/17/19
to BeagleBoard
Hi,

i am new to beagle bone black board..

i flashed new Debian Image to BBB and worked on simple LED blinking program in C. it is working fine(external LED connected in bread board), in Linux ubuntu

next example i am trying to control that external LED using external switch :

like this :

system("echo out > /sys/class/gpio/gpio68/direction) for LED.
system ("echo in > /sys/class/gpio/gpio67/direction) for switch.

using these coding lines i am making LED high and Low in C file
system("echo 1 > /sys/class/gpio/gpio68/value)
system("echo 0 > /sys/class/gpio/gpio68/value)

based on the switch status i need to make LED ON and LED OFF...

how to read the state of GPIO in beagle bone black in C file???

please help me any one ....

Rob Parsons

unread,
Jul 17, 2019, 1:22:11 AM7/17/19
to beagl...@googlegroups.com
The easy way is going to be reading the value file for the gpio pin you have set as input. In C:

FILE * f = fopen("/sys/class/gpio/gpio67/value", "r");
int val;
(void) fscanf(f, "%d", &val);

tada the variable val now holds a one or zero based on the state of the gpio pin.


For simple stuff like this, usually a quick Google search will have the answers ;)


--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/42542757-ff67-4183-99f1-d54834768d60%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Megha Bhirade

unread,
Jul 17, 2019, 3:02:59 AM7/17/19
to BeagleBoard

Hi,

thanks for reply, i understand the concept and hardware connections from that link what u shared...

Let me check once working of program to control the LED blinking using push botton

Robert Heller

unread,
Jul 17, 2019, 8:29:24 AM7/17/19
to beagl...@googlegroups.com, BeagleBoard, Robert Heller
At Tue, 16 Jul 2019 21:52:50 -0700 (PDT) beagl...@googlegroups.com wrote:

>
>
>
> Hi,
>
> i am new to beagle bone black board..
>
> i flashed new Debian Image to BBB and worked on simple LED blinking program
> in C. it is working fine(external LED connected in bread board), in Linux
> ubuntu
>
> next example i am trying to control that external LED using external switch
> :
>
> like this :
>
> system("echo out > /sys/class/gpio/gpio68/direction) for LED.
> system ("echo in > /sys/class/gpio/gpio67/direction) for switch.

You don't really need to fork a process like that. You can just do this
direcly in C:

#include <stdio.h>

typedef enum direction {in, out} Direction;

void SetDirection(int GPIO, Direction dir)
{
char filename[36];
FILE *fp;

snprintf(filename,sizeof(filename),"/sys/class/gpio/gpio%d/direction",GPIO);
fp = fopen(filename,"w");
switch (dir) {
case in: fprintf(fp,"in\n"); break;
case out: fprintf(fp,"out\n"); break;
}
close(fp);
}

Then in your code:

#define LED 68
#define SWITCH 67

SetDirection(LED,out);
SetDirection(SWITCH,in);

>
> using these coding lines i am making LED high and Low in C file
> system("echo 1 > /sys/class/gpio/gpio68/value)
> system("echo 0 > /sys/class/gpio/gpio68/value)
>

typedef enum value {LOW=0, HIGH=1} Value;

void WriteGPIO(int GPIO, Value v)
{
char filename[32];
FILE *fp;

snprintf(filename,sizeof(filename),"/sys/class/gpio/gpio%d/value",GPIO);
fp = fopen(filename,"w");
fprintf(fp,"%d\n",(int) v);
close(fp);
}

Then in your main program:

WriteGPIO(LED,HIGH); /* Led on */
WriteGPIO(LED,LOW); /* Led off */


> based on the switch status i need to make LED ON and LED OFF...
>
> how to read the state of GPIO in beagle bone black in C file???

How about this:

Value ReadGPIO(int GPIO)
{
char filename[32];
FILE *fp;
Value v;

snprintf(filename,sizeof(filename),"/sys/class/gpio/gpio%d/value",GPIO);
fp = fopen(filename,"r");
fscanf(fp,"%d",(int *)&v);
close(fp);
return (v);
}

Then:

if (ReadGPIO(SWITCH) == HIGH) {
/* switch input is high ... */
}

>
> please help me any one ....
>

--
Robert Heller -- 978-544-6933
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
hel...@deepsoft.com -- Webhosting Services

TJF

unread,
Jul 17, 2019, 10:28:03 AM7/17/19
to BeagleBoard
Insiders tip welcome?

Compiling against libpruio this is a 10-liner. Check out the examples section of the docs.

Regards

Megha Bhirade

unread,
Jul 18, 2019, 6:35:19 AM7/18/19
to BeagleBoard


Hi TJF and Robert and Rob

it is working fine. i am able to control the LED blinking by externally connected switch..

Thank to all  for reply and guiding me.
Reply all
Reply to author
Forward
0 new messages