IoT Bytes

Bits and Bytes of IoT

Control Raspberry Pi GPIO Pins with piCore and RPi.GPIO

Pradeep Singh | 2nd Sep 2017

pins

If you have installed piCore on your Raspberry Pi and are looking to control GPIO Pins using the python scripts, you can follow this article. Here, I am using a Raspberry Pi 3 and “piCore-9.0.3.zip” piCore image downloaded from this link – “http://distro.ibiblio.org/tinycorelinux/9.x/armv6/releases/RPi/“.

1. Install Packages:

1.1 Install Python using the following command –

tce-load -wi python3.5.tcz

If you want to use any other Python package use “tce-ab” command to search other “.tcz” files.

1.2 Install Python RPI.GPIO package using the following command –

tce-load -wi python-RPi.GPIO.tcz

2. Prepare the Circuit:

For this example you need, Raspberry Pi 2/3, a LED and a 330 or 220 Ohm Resistor.

Connect LED anode (+) with 330 Ohm (or 220 Ohm) resistor to Pin # 8 on Raspberry Pi 3 (GPIO # 14), and connect LED cathode (-) to Ground Pin # 6. For more details refer to the following picture –

AWS_Shadow_LED

3. Create a Python Script to Blink the LED:

To organize your python scripts create a dedicated directory under “/home/tc” using the following command –

mkdir /home/tc/python_scripts

Create a new Python Script in “python_scripts” directory with the following command –

vi /home/tc/python_scripts/led_blinker.py

Copy and Paste following code into “vi” editor window and save the file –

import RPi.GPIO as GPIO
import time

# Configure the PIN # 8
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8, GPIO.OUT)
GPIO.setwarnings(False)

# Blink Parameters
blink_interval = .5 #Time interval in Seconds
blink_Count = 10

# Blinker Loop
for i in range(0,blink_Count):
 GPIO.output(8, True)
 time.sleep(blink_interval)
 GPIO.output(8, False)
 time.sleep(blink_interval)

# Release Resources
GPIO.cleanup()

4. Execute the Script to Blink the LED:

Execute the python script using the following command –

sudo python /home/tc/python_scripts/led_blinker.py 

During the execution of this python script, You should see your LED blinking. if not, check the connections on your Raspberry Pi.

5. Make the Script Persistent:

If you reboot the Raspberry Pi, you would lose this python script. If you want to make it persistent, following these steps –

5.1 Use the following command to add the “python_scripts” directory in the list of file/directories that should be backed up by “filetool.sh” script –

sudo echo '/home/tc/python_scripts' >> /opt/.filetool.lst

5.2 (optional Step) If you want to trigger this script on system startup, add it to “bootlocal.sh” file using the following command –

sudo echo 'sudo python /home/tc/python_scripts/led_blinker.py' >> /opt/bootlocal.sh

5.3 Save the persistent data using the following command –

filetool.sh -b

After this step, if you reboot your Raspberry Pi you should not lose the “led_blinker.py” script.


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: