using Bash script to control LED with WiringPi on Raspberry Pi

Before programming in bash

You need is to install WiringPi first.

Click here for information of installing WiringPi on Raspberry Pi.


SInce bash is already installed on most Linux destros. we just need to move on.

Prepare Circuit

Like what I did before, the short leg of LED light is connect to the GND of the pin pool of Raspberry Pi board.

And I used a 4.7K resistor to connect with between the GPIO 18 and the long leg of the LED light.


Write a bash script

vim blink.sh

then we need to write these down:

gpio -g mode 18 out             #set pin 18 mode out
gpio -g write 18 1              #set pin 18 on
while true; do
        gpio -g write 18 0      #LED light off
        echo LED light is off
        sleep 1
        gpio -g write 18 1      #LED light on
        echo LED light is on
        sleep 1
done

This script includes an infinite loop.

Make the shell script executable

chmod +x blink.sh

Run the script

./blink.sh

You will see:

pi@raspberrypi ~/songhua $ ./blink.sh 
LED light is off
LED light is on
LED light is off
LED light is on
LED light is off
LED light is on
LED light is off
LED light is on
LED light is off
LED light is on
LED light is off
LED light is on
LED light is off
LED light is on
LED light is off
LED light is on
^Cpi@raspberrypi ~/songhua $ 

using " CTRL + C " for key interrupt to terminate the present processl


Please inform the author or declare the original link of this page when you share with others. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值