atomic3
Posts: 99
Joined: Thu Jan 17, 2013 4:31 pm

GPIO with bash

Wed May 08, 2013 12:54 am

I'd like to use bash to control the gpio pins on the pi.

I've found a couple of examples to change the state of the pins and read values from them.

I was wondering if it would be possible to hook up and IR Receiver and try to read the output with just bash.

Also generating PWM with bash would involve alternating on/off with sleep intervals correct?

Thanks.

User avatar
joan
Posts: 14887
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: GPIO with bash

Wed May 08, 2013 7:06 am

I was wondering if it would be possible to hook up and IR Receiver and try to read the output with just bash.
You could certainly try. ;)

You won't get any useful results though. The pulses are of the order of 100s of microseconds. I don't see how you'd time them in bash.
Also generating PWM with bash would involve alternating on/off with sleep intervals correct?
Yes, that's what PWM does. Don't expect great speeds though.

See http://codeandlife.com/2012/07/03/bench ... pio-speed/

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: GPIO with bash

Wed May 08, 2013 12:19 pm

I use GPIO from BASH, using Gordons WiringPi Library. https://projects.drogon.net/raspberry-pi/wiringpi/ . You can do much direct from BASH using the gpio utility (including using PWM) , but its not too difficult to write code using his library to do timing critical and 'messy' bits that BASH doesn't do too well. Gordon does a fair amount of posting on this forum.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

atomic3
Posts: 99
Joined: Thu Jan 17, 2013 4:31 pm

Re: GPIO with bash

Wed May 08, 2013 2:34 pm

Thanks for your replies.

I just don't like to install a lot of stuff and really wanted to just use bare Bash, I guess I'll get wiringPI a try.

RPiJunior
Posts: 21
Joined: Tue May 07, 2013 6:12 pm

Re: GPIO with bash

Thu May 09, 2013 6:39 am

Run

Code: Select all

echo [pin number] > /sys/class/gpio/export
replacing [pin number] appropriately.

For output,

Code: Select all

echo out > /sys/class/gpio/gpio[pin number]/direction
replacing [pin number] appropriately.

For input,

Code: Select all

echo in > /sys/class/gpio/gpio[pin number]/direction
replacing [pin number] appropriately.

Code: Select all

/sys/class/gpio/gpio[pin number]/value
replacing [pin number] appropriately, is your man.

Run

Code: Select all

echo [pin number] > /sys/class/gpio/export
replacing [pin number] appropriately, to clean up.

:mrgreen: :mrgreen:

Code: Select all

int main() {
    try
        throw "off table";
    } catch (void e) {
        printf("Phew...");
    }
}
WARNING: I destroy! :twisted:

Return to “Beginners”