GPIO LED blinker using SYSFS on the Rock64
#8
   

In this installment of the gpio blog I've included an input pin; a gpio input pin is fun and useful. In the pic above I have replicated the gpio_LED_lab on the Rock64 and I'm using both the PI-2 bus, and the PI P5+ bus as well.  

All of the codes in this section are snippets of bash scripts, using the sysfs exclusively; in later installments we'll switch over to Python3 and then finally to C|C++. 

The following PI-2 bus pin resources are used :

pin(15)   gpio3_a4    gpio100   output
pin(16)   gpio3_a5    gpio101   output
pin(18)   gpio3_a6    gpio102   output
pin(22)   gpio3_a7    gpio103   output

The following PI-P5+ bus pin resources are used :

pin(6)    gpio2_c3    gpio83    input
pin(21)   gpio2_d1    gpio89    output
pin(22)   gpio2_d0    gpio88    output

After exporting a gpio# to set the pin as input set the direction to "in":

  echo in > /sys/class/gpio/gpio83/direction

Doing so places two additional file elements in gpio83/ called "active_low" and "edge". Edge is used if you were to want to use the pin as an interreupt (all gpios can be used as interrupt pins). The "active_low" (default 0) sets the pullup or pulldown of the pin.  The default is to pullup the pin to "1" normally.  To make the pin active, the hardware (your button) pulls the pin down to ground potential and while holding gives the pin a value of zero "0". If you set the active_low to "1" the opposite is true;  the pin is pulled down to "0" and your button then would need to pull the pin up to 3v3 to set it "active" "1".

To read the input pin in our bash script we set a variable using back-ticks (`) and then 'cat' the value to the bash variable:

  VARIABLE=`cat /sys/class/gpio/gpio83/value`

Note:  the back-ticks (`) are not single quotes;  the back-tick is the character under the tilde (~) on most keyboards.  The back-ticks signal bash to run the 'cat' command to get the value, and then place the value in the VARIABLE.

In the following hardcoded snippet I'm going to be running our friend the blinker.sh code to run a controlled loop that will blink pin gpio100;  only this time the code will run until the input pin gpio83 is triggered by pressing our button on the gpio_LED_lab.  

Code:
COUNTER=1
while [ $COUNTER -gt 0 ]; do
  echo 1 > /sys/class/gpio/gpio100/value
  sleep .35
  echo 0 > /sys/class/gpio/gpio100/value
  sleep .65
  COUNTER=`cat /sys/class/gpio/gpio83/value`
done


The code can be run in the foreground or background, regardless; the code will run continuously until such time as we press and hold the button which 'grounds' pin gpio83 ( its value will be read as a zero 0 causing the while loop to terminate ).

The code works because once every time through the blink loop the value of gpio83 is read, and then if $COUNTER is set to zero 0 the while loop exits. If we read the value at the bottom of the loop the LED will be "left" off.  If we read the value in the middle of the loop (after the set ON) then the LED will be "left" ON.

Note:  It is often handy to have even a simple gpio_LED_lab while developing gpio projects (once you get the LEDs right, you can move on to the real objects of your project-- like relays or motors;



Here is the real sysled.sh that runs on my Rock64 as a heart-beat :


sysled.sh

Code:
#!/bin/sh

COUNTER=1
while [ $COUNTER -gt 0 ]; do
  echo 1 > /sys/class/gpio/gpio$1/value
  sleep $3
  echo 0 > /sys/class/gpio/gpio$1/value
  sleep $4
  COUNTER=`cat /sys/class/gpio/gpio$2/value`
done

To run the above codes:

./sysled.sh  103  83  .40  .75  &

The code will blink forever gpio103 with timing values (.40 ON) (.75 OFF) in the background (&)  until the button is pressed on the gpio83 input pin !

This code snippet is useful for knowing that the system is running;  the LED slowly blinks indicating that the processor is active.

Note:  Its fun to start more than one sysled.sh process active (yes, that's possible) each on a different LED !  Then set different timing values for each LED.

Note2:  Of course, the above code snippet assumes you have setup the gpio pins ahead of time;  I have a routine in my startup that activates the gpio pins that I will be using for input and output.

   

Note3:  you don't need the variable in this code;  the read of the input pin can be placed in the while condition directly without the use of an intermediate variable:

sysled2.sh

Code:
#!/bin/sh

while [ `cat /sys/class/gpio/gpio$2/value` -gt 0 ]; do
  echo 1 > /sys/class/gpio/gpio$1/value
  sleep $3
  echo 0 > /sys/class/gpio/gpio$1/value
  sleep $4
done
marcushh777    Cool

please join us for a chat @  irc.pine64.xyz:6667   or ssl  irc.pine64.xyz:6697

( I regret that I am not able to respond to personal messages;  let's meet on irc! )


Messages In This Thread
RE: GPIO LED blinker using SYSFS on the Rock64 - by MarkHaysHarris777 - 07-10-2017, 12:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Rock64 No Audio @ Debian 12 dmitrymyadzelets 2 333 04-08-2024, 06:47 AM
Last Post: dmitrymyadzelets
  OpenWRT on the Rock64 CanadianBacon 14 8,448 04-03-2024, 08:48 AM
Last Post: helpmerock
  Rock64 bricked shawwwn 7 5,770 03-17-2024, 12:22 PM
Last Post: dmitrymyadzelets
  Rock64 won't boot luminosity7 10 4,281 03-16-2024, 08:33 AM
Last Post: dmitrymyadzelets
  Rock64 doesn't boot dstallmo 1 360 03-16-2024, 08:29 AM
Last Post: dmitrymyadzelets
  How well does Rock64 deal with HDR and Atmos on Kodi? drvlikhell 3 1,963 04-29-2023, 04:24 AM
Last Post: newestssd
  Rock64 board not working, no HDMI no Ethernet. EDited 3 3,577 01-17-2023, 02:31 PM
Last Post: Flagtrax
  ROCK64 v3 can it boot from USB? Tsagualsa 4 2,159 11-29-2022, 11:31 AM
Last Post: Macgyver
  rock64 v3 spiflash Macgyver 0 775 11-28-2022, 02:18 PM
Last Post: Macgyver
  my rock64 dosen't work rookie_267 0 971 10-07-2022, 07:50 PM
Last Post: rookie_267

Forum Jump:


Users browsing this thread: 2 Guest(s)