Saturday, February 24, 2018

Raspberry PI GPIO Test

GPIO Test Using Raspberry Pi


This post will talk about how fast raspberry pi can trigger GPIO. 
I did not want to investigate it deeply here but just wanted to quickly check it.
So, I decided to use the well maintained python GPIO library for raspberry pi.
For this test, I wired the output pin to input pin and measure the loopback time using timers.


Setup
Connect GPIO pins:
In my case, using GPIO16(pin #36) as output and GPIO20(pin #38) as input.
I used female jumper wires to connect those pins.
GPIO pin numbering diagram

Notes:

The GPIO software module for python is installed by default if you are using Raspbian.
If you want to double check the versions and/or install the latest version, refer to this site.


Python Code

Python code is pretty simple.
You call "import RPi.GPIO as GPIO" in the beginning and set the mode.
This is the python example code.
Example:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def my_callback(channel):
              end=time.time()
              elapsed=(end-start)*1000
              print(str(elapsed) + " ms")
raw_input("Press Enter\n")
GPIO.add_event_detect(20, GPIO.FALLING, callback=my_callback, bouncetime=300)
try:
              while True:
                           GPIO.output(16, 1)
                           time.sleep(0.5)
                           start=time.time()
                           GPIO.output(16, 0)
                           time.sleep(0.5)
except KeyboardInterrupt:
              GPIO.cleanup()

Notes:
There are 2 types of mode settings.
GPIO.BOARD means it is referring to the pins by the number of the pin.
GPIO.BCM means it is referring to the pins by the "Broadcom SOC channel" number.
For the details, you can refer to this great article here.

Crate a python script file as below and copy&paste the above code.
nano test.py
Run the python script.
python test.py

Result

The result was quite interesting.
It was around 0.2ms.
I think this is not a problem for the projects I do.

Done!



                  

No comments:

Post a Comment