Raspberry Pi Learning Note--shell script operation Gpio

Source: Internet
Author: User
Tags chmod

0. Preface

Raspberry Pi can be painless starter Linux, which controls the Raspberry Pi gpio through command and shell scripting. With this article you can familiarize yourself with multiple Linux commands and familiarize yourself with the shell scripting methods and have a basic understanding of IO redirection.          I learned in the process of Raspberry Pi accumulated confidence, now feel that Linux is not so mysterious not so difficult, but full of endless fun, hope that through the blog to bring happiness to everyone. Related blog "Raspberry Pi learning note--gpio functional learning" Raspberry Pi Gpio starter article

1. Pin-to-number relationship
2. Using commands to manipulate GpioPlease enter the following commands patiently and see the effects
# Get Super Permissions
sudo su
# into the Gpio directory
cd/sys/class/gpio
# to view the contents of the Gpio directory using LS, you can view the
# export Gpiochip0 Unexport 
LS
# Gpio operation interface exposed from kernel space to user space
# After doing this, a Gpio4 file is added to the directory
, echo 4 > Export
# into the GPIO4 directory, This directory is generated by the previous Operation
CD Gpio4
#   View the contents of the Gpio4 directory, you can view the
#   Active_low Direction Edge Power Subsystem Uevent value
ls 
# set GPIO4 for output direction
echo out > Direction
# Bcm_gpio4 Output logic high, LED lit
echo 1 > Value
#  Bcm_gpio4 output Logic low, led off
echo 0 > Value
# Return to the previous level directory
CD: /
# Logout GPIO4 interface
echo 4 > Unexport
# Quit Super User
exit

A few notes

"1" > symbol for IO redirection symbol, IO redirection is the default device that changes the Linux standard input and output, pointing to a user-defined device. For example, echo 4 > Export writes 4 to the export file. "Data" on Gpio drivers in the "2" Linux kernel.

3. Write a shell script

Create a new script named Ledon.sh.

#! / bin / bash
#Use echo to output some prompt statements
echo Export pin
echo> / sys / class / gpio / export

echo setting Direction to output
echo off> / sys / class / gpio / gpio $ 1 / direction

echo setting pi high
echo 1> / sys / clas S / gpio / gpio $ 1 / value


A few notes

"1" If you write a shell script on windows (and then transfer to the Raspberry Pi using the FTP tool), you must save it in UNIX format. The shell script ends with \ n per line, not \ r \ n in Windows. You can use the notepad++ format conversion feature to complete the change. "2" In general, script files are readable and writable, but cannot be executed. You need to add executable functionality through the CHMOD directive, such as entering the following command (note that you need to enter the directory where the shell script is located via the CD command) chmod +x ledon.sh "3" shell script can pass in parameters, for example, $ $ for the first parameter, and $ for the second argument, And so on

Run the script to enter the following instructions to make the Bcm_gpio4 output high-level sudo./ledon.sh 4

4. Summary

In fact, the Raspberry Pi Gpio control is so simple. 

5. References

"1" book "In Love with Raspberry Pi"



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.