2

How do I get the state of a pin and store it in a variable in a script?

2

You could try something like this if you are wired to pin 0 (as referred to in this site https://projects.drogon.net/raspberry-pi/wiringpi/pins/), which will read the pin once, set the variable "b", echo to console and exit.

#!/bin/bash

# Set the pin for input
gpio mode 0 in

# Set the variable through command substitution
b=$(gpio read 0)

# Echo the value to the console
echo "$b"
1

See http://elinux.org/Rpi_Low-level_peripherals#Bash_shell_script.2C_using_sysfs.2C_part_of_the_raspbian_operating_system

This gives examples of how to read the value of a GPIO pin.

cat /sys/class/gpio/gpio7/value 

I am not sure how to set a value in a script I would try

export PIN=`cat /sys/class/gpio/gpio7/value`

I don't know why you would want to do this, as it would seem more logical to read the current value each time.

The section following in the link shows how to use gpio, which is much easier to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.