Monday, October 31, 2016

oDroid C2 GPIO Setup

Hardware: oDroid C2 Rev0.2 20151218 / SanDisk Extreme SD

Software: Ubuntu 64 16.04 Minimal (ubuntu64-16.04-minimal-odroid-c2-20160815)

As embedded projects tend to go, a new requirement popped up and forced me at the last minute to throw out an Arduino solution and replace it with one of the oDroid C2's and SanDisk Extreme SD cards (https://www.amazon.com/gp/product/B00M55BX3G) I have on the shelf for another project.  Of course this also meant rewriting every stinking piece of code in Python vs. C.

The last minute time crunch meant that I wasted precious hours reading piecemeal discussions on how to get simple pin I/O working on the oDroid series of boards.  Now that the fire is out, I drafted up the following instructions below for future reference.



Software Setup


  1. Download Ubuntu Minimal oDroid-specific install:
  2. If you're using Windows then unzip using 7-Zip (it might say it can't do it - ignore it and open it from inside the 7-zip console)
  3. If using Windows then flash the SD card media using Win32DiskImager
  4. Install/boot from SD card with oDroid
  5. Login using default login: 
    • user: root
    • password: odroid
  6. type in each command below followed by <enter>
    1. sudo apt-get install python-dev python-setuptools
    2. sudo apt-get install git-core
    3. git clone https://github.com/hardkernel/WiringPi2-Python.git
    4. cd WiringPi2-Python
    5. git submodule init
    6. git submodule update
    7. sudo python setup.py install
    8. vi btnTest.py
  7. press i and type in the following code:
#!/usr/bin/python

import wiringpi2 as wpi
import time

wpi.wiringPiSetup()
# GPIO pin 0 (physical pin 11) setup for Input
wpi.pinMode(0, 0)  # (pin, input/output)

btnValue=1 
while btnValue == 1:
time.sleep(0.1)
# Read the button value
btnValue = wpi.digitalRead(0)
print btnValue


Press <Esc>, then :wq, followed by <Enter>

Wiring Setup


Wire up a simple button connected to the odroid GPIO using the reference at:
Try not to punch the wall when you realize all of the various virtual numbering schemes that you're going to have to eventually keep track of on all the GPIO pins.

Connect everything similar to the outline below:
  1. wire from GND (e.g. pin 39 or other GND pin) 
    • Button standoff
  2. wire from remaining button standoff will split into a Y going to 2 destinations
    • 10k resistor
      • pin 11 (or whatever GPIO programmable pin you want)
    • 1k resistor
      • pin 0 (3.3v supply)
So why do we need these resistors?  For LEDs and other devices it can be about limiting the current flow.  For buttons, that's not the case.  In a nutshell, it is about normalizing results.

Specifically, this is about ensuring that the state change of the button press is clearly recognized.  Essentially the board recognizes a "state change" (HIGH/LOW) when the power it sees coming down the line deviates.  If you don't use the resistor (I used a 10k/1k here but others *might* work for you), then even small variations caused by interference from other electronics or the board itself can be recognized as a button press.  You can even test this yourself by removing the 10k or 1k resistor from the line, running the code as indicated below and seeing what happens.

Also, the wiring here means that a non-pressed button delivers a HIGH value and a pressed one delivers LOW.  So the code above (e.g. while btnValue == 1:) uses a 1 to indicate the button is not pressed and a 0 to indicate a press.  Depending upon your wiring here, this can be easily reversed.

Testing your setup

Assuming you've got it all set up properly, then run the following command followed by <Enter>

  • sudo python btnTest.py


That should now print out a bunch of 1's until you press the button.  At that point it should show a 0 and then terminate.  Note that if you look at the code, hardware pin 11 (on the physical GPIO) is actually pin 0 within the software. Again, see the diagram here:

If your oDroid starts rebooting during this test (particularly at a button press) then you've got something wired wrong.

If your oDroid immediately exits printing a 0 or it prints one or more 1's and then a 0 when you did not press the button then you likely don't have the button resistor (e.g. 10k noted above) properly inline.

No comments:

Post a Comment