{"id":297,"date":"2017-10-21T19:57:02","date_gmt":"2017-10-21T17:57:02","guid":{"rendered":"https:\/\/www.petersplanet.nl\/?p=297"},"modified":"2017-10-25T19:54:54","modified_gmt":"2017-10-25T17:54:54","slug":"experimenting-with-raspberry-pi","status":"publish","type":"post","link":"https:\/\/www.petersplanet.nl\/index.php\/2017\/10\/21\/experimenting-with-raspberry-pi\/","title":{"rendered":"Experimenting with the Raspberry Pi"},"content":{"rendered":"<p>A few years ago I bought a Raspberry Pi but I never found time to experiment with the GPIO bus.<br \/>\n<!--more-->Recently I have bought a breadboard and some parts to experiment. I found a nice tutorial and this is the result.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-298 aligntop\" src=\"https:\/\/www.petersplanet.nl\/wp-content\/uploads\/2017\/10\/IMG_20171021_182113.jpg\" alt=\"\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The source code has been written in Python.<br \/>\nThe red LED is used to test the digital output functionality.<br \/>\nThe green LED is used to test the analog (Pulse Width Modulation) output functionality.<br \/>\nThe yellow switch has been used to test the digtal input functionality.<\/p>\n<pre># External module imports\r\nimport RPi.GPIO as GPIO\r\nimport time\r\n\r\n# PIN definitions\r\n# Digital\r\nledRed = 23 # Broadcom pin 23 (P1 pin 16)\r\nbutton = 17 # Broadcom pin 17 (P1 pin 11)\r\n\r\n# Analog (Pulse Width Modulation)\r\npwmGreen = 18 # Broadcom pin 18 (P1 pin 12)\r\ndc = 95       # Duty cycle (0-100) for PWM pin\r\n\r\n# Pin Setup:\r\nGPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme\r\nGPIO.setup(ledRed, GPIO.OUT)   # Set as output\r\nGPIO.setup(pwmGreen, GPIO.OUT) # Set as output\r\npwm = GPIO.PWM(pwmGreen,100)  # Initialize PWM on pwmGreen with 100Hz frequency\r\nGPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w\/ pull-up\r\n\r\n# Initial state for LEDs:\r\nGPIO.output(ledRed, GPIO.LOW)\r\npwm.start(dc)\r\n\r\nprint(\"Here we go! Press CTRL+C to exit\")\r\ntry:\r\n    while True:\r\n        if GPIO.input(button): # button is released\r\n            pwm.ChangeDutyCycle(dc)         # Green led\r\n            GPIO.output(ledRed, GPIO.LOW)   # Red led\r\n        else: # button is pressed:\r\n            pwm.ChangeDutyCycle(100-dc)     # Green led\r\n            GPIO.output(ledRed, GPIO.HIGH)  # Red led\r\n            time.sleep(0.075)\r\n            GPIO.output(ledRed, GPIO.LOW)   # Red led\r\n            time.sleep(0.075)\r\nexcept KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:\r\n    pwm.stop() # stop PWM\r\n    GPIO.cleanup() # cleanup all GPIO\r\n<\/pre>\n<p>More details can be found on the website of learn.sparkfun.com.<\/p>\n<p>Links:<br \/>\n<a href=\"https:\/\/learn.sparkfun.com\/tutorials\/raspberry-gpio\">https:\/\/learn.sparkfun.com\/tutorials\/raspberry-gpio<\/a><br \/>\n<a href=\"https:\/\/www.raspberrypi.org\/\">https:\/\/www.raspberrypi.org\/<\/a><br \/>\n<a href=\"https:\/\/projects.raspberrypi.org\/en\/projects\/physical-computing\">https:\/\/projects.raspberrypi.org\/en\/projects\/physical-computing<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few years ago I bought a Raspberry Pi but I never found time to experiment with the GPIO bus.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-297","post","type-post","status-publish","format-standard","hentry","category-linux","category-programming"],"_links":{"self":[{"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/posts\/297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/comments?post=297"}],"version-history":[{"count":27,"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/posts\/297\/revisions"}],"predecessor-version":[{"id":327,"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/posts\/297\/revisions\/327"}],"wp:attachment":[{"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/media?parent=297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/categories?post=297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.petersplanet.nl\/index.php\/wp-json\/wp\/v2\/tags?post=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}