{"id":1711,"date":"2015-07-05T23:05:03","date_gmt":"2015-07-05T20:05:03","guid":{"rendered":"http:\/\/saisa.eu\/blogs\/Guidance\/?p=1711"},"modified":"2015-07-11T20:29:58","modified_gmt":"2015-07-11T17:29:58","slug":"raspberry-pi-and-physical-interfaces","status":"publish","type":"post","link":"https:\/\/saisa.eu\/blogs\/Guidance\/?p=1711","title":{"rendered":"Raspberry Pi and physical interfaces"},"content":{"rendered":"<table>\n<tr>\n<td valign=\"top\">\n<p>Raspberry Pi contains several <a href=\"https:\/\/www.raspberrypi.org\/documentation\/usage\/gpio\/\">GPIO<\/a> pins, which can be used as digital input or outputs (GPIO = General-purpose input\/output).<\/p>\n<p>For example, if GPIO4 is defined as output, then setting the output value to 1 provides 3.3V into that physical pin 7. If GPIO4 is defined as input, then it can read values 0 or 1. Value 1 means that there is a voltage detected on the physical pin 7.<\/p>\n<p>The same physical connector also has pins for other interfaces, for example for a serial interface <a href=\"https:\/\/www.raspberrypi.org\/documentation\/hardware\/raspberrypi\/spi\/README.md\">SPI<\/a>. (MOSI, MISO, SCLK). The physical pins for SPI are 19, 21, 23-26.<\/p>\n<p>SPI is usefull for reading analog values from outside world via ADC (Analog-to-digital converter). Some ADC chips contains the SPI and can therefore be directly used with Raspberry Pi.<\/p>\n<\/td>\n<td valign=\"top\">\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/elinux.org\/RPi_Low-level_peripherals\"><img loading=\"lazy\" alt=\"Pi-GPIO-header\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2015\/07\/pi-gpio-header.png\" width=\"240\" height=\"411\" \/><\/a><\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p>However, if one wish to connect Raspberry Pi with physical devices, it is easier to buy one of the <a href=\"http:\/\/elinux.org\/RPi_Expansion_Boards\">expansion boards<\/a>. For example, <a href=\"https:\/\/www.raspberrypi.org\/gertboard-is-here\/\">Gertboard<\/a>.<\/p>\n<p>In this blog, we take a simple look on some examples and are using python for programming. Please see <a href=\"http:\/\/elinux.org\/RPi_GPIO_Code_Samples\">RPi GPIO Code Samples<\/a> for more details on programming languages available, for example <a href=\"http:\/\/wiringpi.com\/download-and-install\/\">wiringPi<\/a> is very widely used from different programming languages.<\/p>\n<p><strong>Using push button to turn on LED<\/strong><\/p>\n<p>This is demonstrating on how to use GPIO. This is following the instructions: <a href=\"http:\/\/raspberry.io\/projects\/view\/reading-and-writing-from-gpio-ports-from-python\/\">Reading and writing from GPIO ports from Python<\/a>.<\/p>\n<p>Main components:<\/p>\n<ul>\n<li>push button, 1 LED, 2 resistors (800 ohm), (GPIO connector cable)<\/li>\n<\/ul>\n<p><img loading=\"lazy\" alt=\"RPI-button-led\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2015\/07\/rpi-button-led.png\" width=\"547\" height=\"410\" \/><\/p>\n<p>The interactive python, ipython, was used with the following code:<\/p>\n<pre>\nimport RPi.GPIO as GPIO\nGPIO.setmode(GPIO.BCM)\nGPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)\nGPIO.setup(25, GPIO.OUT, initial=GPIO.LOW)\nGPIO.add_event_detect(4, GPIO.BOTH)\ndef my_callback(channel):\n   GPIO.output(25, GPIO.input(4))\nGPIO.add_event_callback(4, my_callback)\n<\/pre>\n<p>Note, GPIO4 is physical pin 7, and GPIO25 is physical pin 22.<\/p>\n<p><em>When change is detected on GPIO4, then my_callback is executed and output of GPIO25 is set to same value as is read from GPIO4.<\/em><\/p>\n<p>After each run, it is good to give command GPIO.cleanup()<\/p>\n<p><strong>Measuring the temperature<\/strong><\/p>\n<p>In this case ADC is used to measure the output voltage from a temperature sensor. The value from ADC is read via SPI.<\/p>\n<p>Local PC store had components from linksprite. The instructions at the manufacturer site are followed: <a href=\"http:\/\/learn.linksprite.com\/raspberry-pi\/shield\/how-to-use-linker-kit-base-shield-for-raspberry-pi-with-adc-interface-with-python-code\/\">How to use Linker kit Base Shield for Raspberry Pi with ADC Interface with Python Code<\/a>. (It is possible to make this test by using TMP36 and MCP3004 directly instead of using ready made components)<\/p>\n<ul>\n<li><a href=\"http:\/\/linksprite.com\/wiki\/index.php5?title=Thermal_Module\">Thermal Module<\/a>, containing a low voltage temperature sensor <a href=\"http:\/\/www.analog.com\/media\/en\/technical-documentation\/data-sheets\/TMP35_36_37.pdf\">TMP36<\/a>.<\/li>\n<li><a href=\"http:\/\/linksprite.com\/wiki\/index.php5?title=Linker_kit_Base_Shield_for_Raspberry_Pi_with_ADC_Interface\">Base Shield<\/a>, containing an ADC <a href=\"http:\/\/ww1.microchip.com\/downloads\/en\/DeviceDoc\/21295d.pdf\">MCP3004<\/a><\/li>\n<\/ul>\n<p><strong><img loading=\"lazy\" alt=\"RPI-ADC-TMP36-text\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2015\/07\/rpi-adc-tmp36-text.png\" width=\"547\" height=\"410\" \/><\/strong><\/p>\n<p>The original instructions were giving voltage values. The code was modified to get both voltages and temperatures:<\/p>\n<pre>\nimport spidev\nimport time\nimport datetime\n\nadc_channel = 0\nspi = spidev.SpiDev()\nspi.open(0,0)\n\ndef readadc(adcnum):\n# read SPI data from MCP3004 chip, 4 possible adc's (0 thru 3)\n  if adcnum &gt;3 or adcnum &lt;0:\n     return-1\n  r = spi.xfer2([1,8+adcnum &lt;&lt;4,0])\n  adcout = ((r[1] &amp;3) &lt;&lt;8)+r[2]\n  return adcout\n\nwhile True:\n    value=readadc(adc_channel)\n    volts=(value*3.3)\/1024\n    ts = time.time()\n    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')\n    print st\n    print(\"SPI: %4d\/1023 =&gt; %5.3f V\" % (value, volts))\n    temp=(1000*volts-500)\/10\n    print(\"                = %4.1f degree C\" % (temp))\n    print(\"-----------------------------------------------------\")\n    time.sleep(5)\n<\/pre>\n<p>When executed, it gave the following printout<\/p>\n<pre>\npi@raspberrypi ~ $ python .\/temperature.py\n2015-07-05 17:22:10\nSPI:  231\/1023 =&gt; 0.744 V\n                = 24.4 degree C\n-----------------------------------------------------\n2015-07-05 17:22:15\nSPI:  231\/1023 =&gt; 0.744 V\n                = 24.4 degree C\n-----------------------------------------------------\n<\/pre>\n<p>Please note, that the reading from serial interface (SPI) is done with the command spi.<a href=\"https:\/\/pypi.python.org\/pypi\/spidev\">xfer2<\/a>().<\/p>\n<p><strong>Serial connection to Raspberry Pi<\/strong><\/p>\n<p>Raspberry Pi&#8217;s GPIO contains pins for serial communication, and it can be used to connect to the operating system. An USB TTL cable was used. The instructions from <a href=\"https:\/\/learn.adafruit.com\/downloads\/pdf\/adafruits-raspberry-pi-lesson-5-using-a-console-cable.pdf\">Adafruit&#8217;s Raspberry Pi Lesson 5. Using a Console Cable<\/a> were used. Please note that RS232 and TTL has different voltages. (If you intend to use USB-RS232 cable, then an additional RS232-TTL converter is needed as well.)<\/p>\n<p><img loading=\"lazy\" alt=\"RPI-USB-TTL\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2015\/07\/rpi-usb-ttl.png\" width=\"312\" height=\"242\" \/><\/p>\n<p><strong>Continuation<\/strong><\/p>\n<p>Please see <a href=\"http:\/\/saisa.eu\/blogs\/Guidance\/?p=1734\">part 2<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Raspberry Pi contains several GPIO pins, which can be used as digital input or outputs (GPIO = General-purpose input\/output). For example, if GPIO4 is defined as output, then setting the output value to 1 provides 3.3V into that physical pin &hellip; <a href=\"https:\/\/saisa.eu\/blogs\/Guidance\/?p=1711\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[68,74,67,34,63,80,82,51,10],"tags":[],"_links":{"self":[{"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/posts\/1711"}],"collection":[{"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1711"}],"version-history":[{"count":6,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/posts\/1711\/revisions"}],"predecessor-version":[{"id":1735,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/posts\/1711\/revisions\/1735"}],"wp:attachment":[{"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}