{"id":1734,"date":"2015-07-11T20:28:31","date_gmt":"2015-07-11T17:28:31","guid":{"rendered":"http:\/\/saisa.eu\/blogs\/Guidance\/?p=1734"},"modified":"2015-07-11T21:55:55","modified_gmt":"2015-07-11T18:55:55","slug":"raspberry-pi-and-physical-interfaces-part-2","status":"publish","type":"post","link":"https:\/\/saisa.eu\/blogs\/Guidance\/?p=1734","title":{"rendered":"Raspberry Pi and physical interfaces, part 2"},"content":{"rendered":"<p>This blog continues from the previous blog <a href=\"http:\/\/saisa.eu\/blogs\/Guidance\/?p=1711\">Raspberry Pi and physical interfaces<\/a>.<\/p>\n<p><strong>Case: GPIO with 1 LED output and 2 inputs<\/strong><\/p>\n<p>First the push button &amp; LED example is modified to include also another input, a hall switch. For the documentation, tool called <a href=\"http:\/\/saisa.eu\/blogs\/Guidance\/?p=1726\">fritzing<\/a> is used. For the example source code, please see <a href=\"http:\/\/saisa.eu\/blogs\/Guidance\/?p=1726\">blog<\/a>.<\/p>\n<p><img loading=\"lazy\" alt=\"Button-LED-Hall-2 bb\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2015\/07\/button-led-hall-2_bb-2.png\" width=\"450\" height=\"511\" \/><\/p>\n<p>This is based on the sources:<\/p>\n<ul>\n<li><a href=\"http:\/\/raspberry.io\/projects\/view\/reading-and-writing-from-gpio-ports-from-python\/\">Reading and writing from GPIO ports from Python<\/a><\/li>\n<li><a href=\"http:\/\/kropp.ca\/tutorials\/raspberry-pi-hall-effect-switch\/\">Raspberry Pi Hall Effect switch<\/a><\/li>\n<li><a href=\"http:\/\/mchobby.be\/wiki\/index.php?title=Rasp-Hack-Hall\">Rasp-Hack-Hall<\/a><\/li>\n<\/ul>\n<p>These 2 separate tests were combined into one. If either push button is pressed or if the magnet comes close, then LED is turned on. Source file &#8220;test-buttonledhall.py&#8221; is shown below. In this case while loop is used instead of callback function. Hall switch used (<a href=\"http:\/\/www.partco.biz\/verkkokauppa\/datasheet\/tle4905l-inf.pdf\">TLE4905L<\/a>) is unipolar, ie. reacts only to one magnetic pole, and &#8220;conductivity&#8221; is opposite (see datasheet).<\/p>\n<div style=\"WIDTH: 650px; OVERFLOW: auto\">\n<pre>\n#!\/usr\/bin\/env python\n\ntry:\n import RPi.GPIO as GPIO, time\n\n GPIO.setmode(GPIO.BCM)\n\n #inputs\n GPIO_PUSH_BUTTON = 4    # push button\n GPIO_HALL_SENSOR = 24   # Hall switch\n GPIO.setup(GPIO_PUSH_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)    # push button\n GPIO.setup(GPIO_HALL_SENSOR, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)   # Hall switch\n hallActive  = False\n\n #outputs\n GPIO_GREEN_LED   = 25   # LED\n GPIO.setup(GPIO_GREEN_LED,   GPIO.OUT, initial=GPIO.LOW)            # LED\n LEDActive   = False\n\n while True:\n    if( GPIO.input( GPIO_PUSH_BUTTON ) == True ): LEDActive = True\n\n    if( GPIO.input( GPIO_HALL_SENSOR ) == False ): LEDActive = True\n\n    if( GPIO.input( GPIO_PUSH_BUTTON ) == False and GPIO.input( GPIO_HALL_SENSOR ) == True ): LEDActive = False\n\n    GPIO.output( GPIO_GREEN_LED, LEDActive )\n    time.sleep( 0.300 )\n\n\n\nfinally:\n    print('cleanup...')\n    GPIO.cleanup()\n<\/pre>\n<\/div>\n<p>Photo and <a href=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2015\/07\/led-hall.mp4\">video<\/a> are available.<\/p>\n<p><img loading=\"lazy\" alt=\"Button-LED-Hall-2 bb-photo\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2015\/07\/button-led-hall-2_bb-photo.png\" width=\"365\" height=\"274\" \/><\/p>\n<p><strong>Case: GPIO single-bus communication: humidity &amp; temperature sensor AM2301 (DHT21)<\/strong><\/p>\n<p>Sources:<\/p>\n<ul>\n<li><a href=\"http:\/\/thelab.dedicatedcomputing.com\/let-it-rain\/\">Let it rain<\/a><\/li>\n<\/ul>\n<p>Links:<\/p>\n<ul>\n<li><a href=\"https:\/\/learn.adafruit.com\/dht\">Adafruit DHTxx sensors<\/a><\/li>\n<li>Other ways: <a href=\"http:\/\/picoboard.pl\/raspberry-pi-obsluga-czujnika-temperatury-wilgotnosci-dht21-am2301\/\">DHT21 with wiringpi<\/a>, <a href=\"http:\/\/www.seeedstudio.com\/wiki\/Grove_-_Temperature_and_Humidity_Sensor\">Grove &#8211; Temperature and Humidity Sensor<\/a> (I did not test these)<\/li>\n<\/ul>\n<p>The digital communication with this sensor is more complex. In the <a href=\"http:\/\/www.partco.biz\/verkkokauppa\/datasheet\/am2301.pdf\">datasheet<\/a>, it is described as single bus communication. The 0&#8217;s and 1&#8217;s are not communicated by voltage, but by timing (in microseconds) on how long the voltage stays up (see C source <a href=\"https:\/\/github.com\/adafruit\/Adafruit-Raspberry-Pi-Python-Code\/blob\/master\/Adafruit_DHT_Driver\/Adafruit_DHT.c\">code<\/a> for more information). Luckily the link above contained a link to an <a href=\"https:\/\/github.com\/adafruit\/Adafruit-Raspberry-Pi-Python-Code\/blob\/master\/Adafruit_DHT_Driver\/Adafruit_DHT\">executable<\/a>. The purpose was to do a quick test if this sensor can be used with Raspberry Pi, and yes it works.<\/p>\n<p>The execution was done with DHT22 or AM2302 as parameter<\/p>\n<pre>\npi@raspberrypi ~ $ sudo .\/Adafruit_DHT 22 4\nUsing pin #4\n        Data (48): 0x1 0xcd 0x0 0xf1 0xbf\nTemp =  24.1 *C, Hum = 46.1 %\npi@raspberrypi ~ $ sudo .\/Adafruit_DHT 2302 4\nUsing pin #4\nData (48): 0x1 0xc8 0x0 0xf4 0xbd\nTemp =  24.4 *C, Hum = 45.6 %\npi@raspberrypi ~ $ sudo .\/Adafruit_DHT 22 4\n                Using pin #4\nData (48): 0x1 0xc9 0x0 0xf2 0xb4\n<\/pre>\n<p>I opened the cover, but unfortunately the connector is not compatible (inch vs metric systems).<\/p>\n<p><img loading=\"lazy\" alt=\"am2301\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2015\/07\/am2301.png\" width=\"182\" height=\"137\" \/><\/p>\n<p><img loading=\"lazy\" alt=\"humidity-sensor\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2015\/07\/humidity-sensor.png\" width=\"365\" height=\"274\" \/><\/p>\n<p><strong>Case: I2C: Gyroscope and Accelerometer readings (MPU-6050)<\/strong><\/p>\n<p>In this case we are following a set of instructions (see Sources below):<\/p>\n<p>Sources:<\/p>\n<ul>\n<li><a href=\"http:\/\/blog.bitify.co.uk\/2013\/11\/interfacing-raspberry-pi-and-mpu-6050.html\">Interfacing Raspberry Pi and MPU-6050<\/a><\/li>\n<li><a href=\"http:\/\/blog.bitify.co.uk\/2013\/11\/reading-data-from-mpu-6050-on-raspberry.html\">Reading data from the MPU-6050 on the Raspberry Pi<\/a><\/li>\n<li><a href=\"http:\/\/blog.bitify.co.uk\/2013\/11\/3d-opengl-visualisation-of-data-from.html\">3D OpenGL visualisation of the data from an MPU-6050 connected to a Raspberry Pi<\/a><\/li>\n<li><a href=\"http:\/\/blog.bitify.co.uk\/2013\/11\/using-complementary-filter-to-combine.html\">Using a complementary filter to combine Accelerometer and Gyroscopic data<\/a><\/li>\n<\/ul>\n<p>Links:<\/p>\n<ul>\n<li><a href=\"http:\/\/playground.arduino.cc\/Main\/MPU-6050\">MPU-6050 Accelerometer + Gyro<\/a><\/li>\n<li><a href=\"http:\/\/diyhacking.com\/arduino-mpu-6050-imu-sensor-tutorial\/\">Arduino MPU 6050 \u2013 Best IMU Sensor Tutorial<\/a><\/li>\n<li><a href=\"http:\/\/www.bitwizard.nl\/wiki\/index.php\/MPU-6050_sensor_connected_to_Raspberry_Pi\">MPU-6050 sensor connected to Raspberry Pi<\/a><\/li>\n<\/ul>\n<p>Verifying I2C connection:<\/p>\n<pre>\npi@raspberrypi ~ $ sudo i2cdetect -y 1\n     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f\n00:          -- -- -- -- -- -- -- -- -- -- -- -- --\n10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --\n70: -- -- -- -- -- -- -- --\n\npi@raspberrypi ~ $ sudo i2cget -y 1 0x68 0x75\n0x68\n<\/pre>\n<p>The picture below shows the sensor and 4 cables used to connect it with Raspberry Pi.<\/p>\n<p><img loading=\"lazy\" alt=\"gyro1\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2015\/07\/gyro1.png\" width=\"365\" height=\"274\" \/><\/p>\n<p>Copying the source code into gyro.py, and executing it (for the source code, see links above):<\/p>\n<pre>\npi@raspberrypi ~ $ sudo .\/gyro.py\ngyro data\n---------\ngyro_xout:  -141  scaled:  -2\ngyro_yout:  328  scaled:  2\ngyro_zout:  63  scaled:  0\n\naccelerometer data\n------------------\naccel_xout:  4628  scaled:  0.282470703125\naccel_yout:  -296  scaled:  -0.01806640625\naccel_zout:  15256  scaled:  0.93115234375\nx rotation:  -1.06367143706\ny rotation:  -16.8724967644\n<\/pre>\n<p>After this, xubuntu VM was used to execute the OpenGL 3D program (level.py) to demonstrate the orientation.<\/p>\n<p><img loading=\"lazy\" alt=\"gyro2\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2015\/07\/gyro2.png\" width=\"342\" height=\"455\" \/><\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>Already &#8220;now&#8221; there are several working examples for testing and playing with &#8220;Internet of Things&#8221; \ud83d\ude42 . Well, here &#8220;now&#8221; actually means 2-3 years back. <u>Now<\/u> there is even more complex examples. But to get started, it is good to start from the basics&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog continues from the previous blog Raspberry Pi and physical interfaces. Case: GPIO with 1 LED output and 2 inputs First the push button &amp; LED example is modified to include also another input, a hall switch. For the &hellip; <a href=\"https:\/\/saisa.eu\/blogs\/Guidance\/?p=1734\">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],"tags":[],"_links":{"self":[{"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/posts\/1734"}],"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=1734"}],"version-history":[{"count":1,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/posts\/1734\/revisions"}],"predecessor-version":[{"id":1738,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/posts\/1734\/revisions\/1738"}],"wp:attachment":[{"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}