timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

GPIO using Bash - help

Tue Jan 01, 2013 12:22 pm

Well the wealth of info I've read has just confused me ! Now duly terrified, Could any one point me in the right direction.

I've got a RPi with Raspbian, fully updated, and a simple IO card hung on the GPIO.

For various reasons I want to use Bash.

So do I need to install anything before I start to turn a pin on or read the voltage on an input ?

I'm very 'literal' so

Do I need to type

sudo wiringinfo bla bla

or what ?

To set a pin high (+5V) or it it (+3.3V), what do I type next ?

Once I've got my multimeter displaying by Bash control, I can pop it all in a script and I'll be very very happy !

Any thoughts will be very very welcome !

:D :D :D

Tim

User avatar
aTao
Posts: 1084
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: GPIO using Bash - help

Tue Jan 01, 2013 12:37 pm

timmoore46 wrote:Well the wealth of info I've read has just confused me ! Now duly terrified, Could any one point me in the right direction.

I've got a RPi with Raspbian, fully updated, and a simple IO card hung on the GPIO.

For various reasons I want to use Bash.

So do I need to install anything before I start to turn a pin on or read the voltage on an input ?
No its all there, everything is a file. Have a look at http://elinux.org/Rpi_Low-level_periphe ... _system.29 while that is a bash command file, you can just type it in (but you knew that....), you do have to sudo though since that part of the file system is admin.

I'm very 'literal' so

Do I need to type

sudo wiringinfo bla bla

or what ?
You can use the file method from the command line, but also WiringPi is set for command line control a well.

To set a pin high (+5V) or it it (+3.3V), what do I type next ?
+3V3! ! because 5V0 anywhere near a GPIO will frazzle your Pi.

Once I've got my multimeter displaying by Bash control, I can pop it all in a script and I'll be very very happy !

Any thoughts will be very very welcome !

:D :D :D

Tim
>)))'><'(((<

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: GPIO using Bash - help

Tue Jan 01, 2013 1:00 pm

So everything I need is within ? :-

#!/bin/sh

# GPIO numbers should be from this list
# 0, 1, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23, 24, 25

# Note that the GPIO numbers that you program here refer to the pins
# of the BCM2835 and *not* the numbers on the pin header.
# So, if you want to activate GPIO7 on the header you should be
# using GPIO4 in this script. Likewise if you want to activate GPIO0
# on the header you should be using GPIO17 here.

# Set up GPIO 4 and set to output
echo "4" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio4/direction

# Set up GPIO 7 and set to input
echo "7" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio7/direction

# Write output
echo "1" > /sys/class/gpio/gpio4/value

# Read from input
cat /sys/class/gpio/gpio7/value

# Clean up
echo "4" > /sys/class/gpio/unexport
echo "7" > /sys/class/gpio/unexport

end of quote

Also what is the syntax of getting 'WiringPi' downloaded /installed ?

Many many thanks for your help !

:D :D :D :D

Tim

pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Re: GPIO using Bash - help

Tue Jan 01, 2013 1:19 pm

timmoore46 wrote: Also what is the syntax of getting 'WiringPi' downloaded /installed ?
Google.

https://projects.drogon.net/raspberry-p ... d-install/
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: GPIO using Bash - help

Tue Jan 01, 2013 2:17 pm

wiringPi installed fine following the instructions,

I'm in the pi directory

and at the bash prompt I type

sudo echo "4" > /sys/class/gpio/export

and I get rewarded as a bash prompt with

no such file or directory

________________________________

guess I'm missing something obvious ?

:( :( :( :(

An thoughts ?

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: GPIO using Bash - help

Tue Jan 01, 2013 2:26 pm

I just typed

sudo -i

and that seems to have fixed it !

:D :D :D

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: GPIO using Bash - help

Tue Jan 01, 2013 3:07 pm

Not entirely sure if I've got the mapping right.

GPIO 4 emereges as gpio7

GPIO 17 emerges as gpio0

As I've got marked gpio0 to 7

what starts for gpio1 to 6 ?

hope that makes sense ?

:D :D :D

Tim

User avatar
aTao
Posts: 1084
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: GPIO using Bash - help

Tue Jan 01, 2013 4:07 pm

timmoore46 wrote:Not entirely sure if I've got the mapping right.

GPIO 4 emereges as gpio7

GPIO 17 emerges as gpio0

As I've got marked gpio0 to 7

what starts for gpio1 to 6 ?

hope that makes sense ?

:D :D :D

Tim
Total sense. what you type refers to the pin numbering on the Broadcom Soc chip. The GPIO header plug is not wired pin for pin, so it has different numbering. See http://elinux.org/Rpi_Low-level_peripherals for the pin numbers on the GPIO header. NOTE read the table carefully noting which board version you have, the signals moved pins with V2.
>)))'><'(((<

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: GPIO using Bash - help

Wed Jan 02, 2013 10:34 am

Many many thanks.

My task of the moment is to Map out the Bash instruction to set a hi or a lo to each of the GPIO port available to me.

I'm using this prototyping board:-

http://www.hobbytronics.co.uk/slice-of-pi

On it there are gp0 through to gp7

So being able to watch each port being set and then changed using Bash, is a simple task (*LOL*)

but lets see if I can manage it !

All I've managed it to observe that port gp7 on the protoboard is 3.3v and nothing I can do changes that.

:D :roll:

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: GPIO using Bash - help

Thu Jan 03, 2013 9:21 am

Now with improve voltage measuring jig and a 4.7k resistor across the probe to GND, I've measured the 16 ports that the proto pcb , I've monitored port7

After powering up and loging in the voltage is 0.00V

I then type

sudo -i

echo "4" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio4/direction
echo "1" > /sys/class/gpio/gpio4/value
________________

and the voltage jumps to 3.26

So many thanks to all that have helped me get here......

now it would be nice to set the voltage back to 0.00V

but my knowledge (or lack of it) runs out of ideas as to what to do next.

(I tried to do the same with other ports but with no positive results so far)

Any thoughts very very welcome !

:D :D :D :D

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: GPIO using Bash - help

Thu Jan 03, 2013 9:29 am

typing

echo "0" > /sys/class/gpio/gpio4/value

fixed the first change and was rewarded with 0.00 volts

so trying another port.....

:D

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: GPIO using Bash - help

Thu Jan 03, 2013 9:41 am

so far port 4, 7 and 10 work fine.

Now anyone any thoughts on the Bash code on inputing the state of a port and sending it to different port ?

Any thoughts would be very very very welcome !

:D :D :D

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: GPIO using Bash - help

Thu Feb 14, 2013 2:14 pm

This gave a unwanted message:

Cat: z: No Such file or directory.

Quote prog

oot@raspberrypi:~# cat control_999
#! /bin/sh
# control_999
# chmod a+x control_999
# control c stops the program

while true
do
#setup Input

echo "7" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio7/direction
cat /sys/class/gpio/gpio7/value > y
cat y

z=1
cat z

echo $z



# setup output

echo "4" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio4/direction
cat y > /sys/class/gpio/gpio4/value
sleep 2

cat z > /sys/class/gpio/gpio4/value
sleep 2

continue
done
root@raspberrypi:~#

end quote.

I know I've made a noddy error , but what or why, I'm not sure.

Brain is frazzed with checking all the GPIO ports as I may have nuked my RPi !

:cry: :cry: :cry: :cry: :cry:

Tim

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: GPIO using Bash - help

Thu Feb 14, 2013 8:29 pm

I think you are trying to misuse 'cat': http://linux.die.net/man/1/cat

So when you do this:

Code: Select all

cat /sys/class/gpio/gpio7/value > y 
cat y 
...you actually create a file named 'y' and then output the file contents with 'cat' - which obviously works as such (even though most likely what you meant...).
But this:

Code: Select all

z=1 
cat z 
...assigns a value '1' to a variable 'z' and the following 'cat' does not find a file named 'z' (as it reports).

You might want to google for 'how to work with variables in bash' and 'how to read file to variable bash' or something along that...
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: GPIO using Bash - help

Fri Feb 15, 2013 9:12 am

That explains all ! Many thanks !

:D :D :D :D

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: GPIO using Bash - help

Sat Feb 16, 2013 8:45 am

What I tried to do when I thought I have nuked my RPi was to send a slow square wave (changed voltage every 3 seconds) to the Header GPIO Output pin 7.

This works:-

root@raspberrypi:~# cat control_output_test_002
#! /bin/sh

# chmod a+x control_output_002

# Outputs to Header Pin 7

# control c stops the program

# Loop the whole script

while true

do


# setup output

echo "4" > /sys/class/gpio/export

echo "out" > /sys/class/gpio/gpio4/direction

echo "1" > /sys/class/gpio/gpio4/value
echo "1"
sleep 3

echo "0" > /sys/class/gpio/gpio4/value
echo "0"
sleep 3

continue
done
root@raspberrypi:~#

_______

Hope this may help others in the same predicament ! (My RPi was OK ! Phew!)

:D :D :D

Tim

laumars
Posts: 1
Joined: Tue Mar 12, 2013 12:12 am

Re: GPIO using Bash - help

Tue Mar 12, 2013 12:17 am

timmoore46 wrote:wiringPi installed fine following the instructions,

I'm in the pi directory

and at the bash prompt I type

sudo echo "4" > /sys/class/gpio/export

and I get rewarded as a bash prompt with

no such file or directory

________________________________

guess I'm missing something obvious ?

:( :( :( :(

An thoughts ?

Tim
For future reference, you cannot do a sudo command > file because what's happening is command is elevated then that shell ends and the output is redirected as the standard user.

So you have to use the tee command: command | tee file

Of course, sudo -i and sudo -s (the latter being better practice) will also work around this issue.

Return to “Beginners”

Who is online

Users browsing this forum: BrianO, DirkS, LTolledo, marci76, nick8967 and 43 guests