<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Raspberry Pi GPIO Explained</title><link>https://community.element14.com/products/raspberry-pi/raspberrypi_projects/w/documents/957/raspberry-pi-gpio-explained</link><description /><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>Raspberry Pi GPIO Explained</title><link>https://community.element14.com/products/raspberry-pi/raspberrypi_projects/w/documents/957/raspberry-pi-gpio-explained</link><pubDate>Mon, 10 May 2021 18:52:36 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:88dc3de6-65c8-42fd-b0c6-a36577feb11a</guid><dc:creator>shabaz</dc:creator><comments>https://community.element14.com/products/raspberry-pi/raspberrypi_projects/w/documents/957/raspberry-pi-gpio-explained#comments</comments><description>Current Revision posted to Documents by shabaz on 5/10/2021 6:52:36 PM&lt;br /&gt;
&lt;h1&gt;Contents&lt;/h1&gt;&lt;p style="margin:0;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/878x485/__key/communityserver-wikis-components-files/00-00-00-01-66/4666.contentimage_5F00_199720.jpg"&gt;&lt;img alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/4666.contentimage_199720.jpg-878x485.jpg?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=C80ddOB5PG1JLMgyuVJ7JN6qj%2BC5pSZ5O%2FmdsJ1W8rg%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=+8lMoj31OJTggMFk8VRL5w==" style="max-height: 485px;max-width: 878px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;h1&gt;Introduction&lt;/h1&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The Raspberry Pi is a computer, and from a connections point of view it doesn’t look much different to a normal desktop computer. It has USB ports for connecting a keyboard and mouse, and a video output port for connecting up a display.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Because it is more compact and lower cost than a large desktop PC, it becomes possible to use the Raspberry Pi or other small single board computers (SBCs) as they are known, for many scenarios where a desktop or laptop PC would not be feasible.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Often you may want to connect up other ‘things’ to a computer. For example you may wish to use a computer to measure the brightness level and automatically control lights, or to sound an alarm if an intruder is detected.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Broken down to more general terms, there is a desire to be able to use a computer to control (also known as to ‘output’) to electronic circuits, and to gain useful information (aka obtain ‘input’) from circuits.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;This is where the Raspberry Pi and other single board computers excel because one key difference between SBCs and desktop or laptop PCs is that SBCs usually have general purpose input/output (GPIO) capability. This is lacking on larger PCs.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/920x517/__key/communityserver-wikis-components-files/00-00-00-01-66/0456.contentimage_5F00_199721.jpg"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/0456.contentimage_199721.jpg-920x517.jpg?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=32exuqey%2FWXWW1pQ0WIb9TiJj1p8XFh%2BSy0DfCRs9T0%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=wE8kgAvYlOBvEHwwhdIazA==" style="max-height: 517px;max-width: 920px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;By making connections to those pins (they are also known as header pins) the Raspberry Pi can interface to the electronic world which consists (amongst other things) of sensors and indicators and actuators.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;This blog post will provide example circuits that can be used as-is (or can be modified and extended) along with code examples in several programming languages. The information is split up into:&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;Connections Overview&lt;/strong&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;Understanding Inputs&lt;/strong&gt; (Use the tabs to work through this section)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;Understanding Outputs&lt;/strong&gt; (Use the tabs to work through this section)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;Some Optional (advanced) topics&lt;/strong&gt; (namely &lt;strong&gt;Speed and Jitter&lt;/strong&gt;; again, use the tabs to work through this section)&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;To prevent too much scrolling, some of the sections are tabbed at the top, so click on the tabs to see more content. Also, click on the photos to zoom in to see wiring detail.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;There is also a short video that contains some extra detail as well as demonstrations of the circuits and code. The video will be available in about a weeks time (bookmark this post if you wish to be notified).&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;h1&gt;Connections Overview&lt;/h1&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The diagram below lists the header pins available on the Raspberry Pi. Pin 1 on circuit boards can typically be identified by looking for the square-shaped pad on the underside of the board. The Raspberry Pi has digital inputs/outputs on its 40-pin connector that comply with 3.3V logic levels. The photo here shows an older Pi 2, but the same connection detail applies for the more recent Pi 3. Notice from the diagram that the pin numbering is in a zig-zag pattern, where pin 1 is lower right, pin 2 is lower left, and then the numbers increase the higher up you go in the zig-zag fashion. Another way of looking at it is to notice that the odd numbered pins are on the right side, and the even numbered pins are on the left side.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/762x644/__key/communityserver-wikis-components-files/00-00-00-01-66/1732.contentimage_5F00_199722.jpg"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/1732.contentimage_199722.jpg-762x644.jpg?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=DaIcxoQFJxR9cCV8b6n9YkT%2FOa5vy%2F4Ms9xJf6nL9Sw%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=3hi0MW8P+tBe30EhSSo7/w==" style="max-height: 644px;max-width: 762px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The term &amp;#39;3.3V logic levels&amp;#39; means that the Raspberry Pi will interpret anything very close to zero volts (it doesn&amp;#39;t have to be precisely 0V) as a logic ‘0’ and anything higher than around 2V as a logic ‘1’. Inputs beyond 3.3V will damage the board. Similarly, when GPIO pins are configured to become outputs, the Raspberry Pi will set the pin to either a voltage close to 0V or a voltage close to 3.3V. Everything in electronics has a tolerance, so (say) measuring 3.21V (as an example) instead of precisely 3.30V is considered acceptable.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;For this blog post&amp;#39;s input/output exercises, the pink, white, red and orange pins on the diagram will be used. The remainder pins are for serial interfacing use which is addressed in other blog posts.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;If you are not using the two 5V pins (bottom-left side, pin numbers 2 and 4) then you could prevent accidental shorts to surrounding pins by placing some plastic sleeving/insulation over them.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;h1&gt;Understanding Raspberry Pi Outputs&lt;/h1&gt;&lt;p style="margin:0;"&gt;&lt;span style="font-size:12pt;"&gt;Work through the three tabs below to get a full understanding of the output functions of your Raspberry Pi.&lt;/span&gt;&lt;/p&gt;&lt;table border="1" class="jiveBorder" style="border:1px solid #000000;width:100%;"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style="border:1px solid black;border:1px solid #000000;padding:2px;color:#ffffff;background-color:#6690bc;text-align:center;" valign="middle"&gt;&lt;strong&gt;{tabbedtable} Tab Label&lt;/strong&gt;&lt;/th&gt;&lt;th style="border:1px solid black;border:1px solid #000000;padding:2px;color:#ffffff;background-color:#6690bc;text-align:center;" valign="middle"&gt;&lt;strong&gt;Tab Content&lt;/strong&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;1) Digital Outputs: LEDs&lt;/td&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;&lt;h1&gt;Getting Started with Digital Outputs: Lighting LEDs&lt;/h1&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;A typical use for output pins is to control an indicator such as an LED or light bulb, or some actuator or motor. Usually an electronic circuit is needed to transform the Raspberry Pi output into something that will control the desired device. For a small LED all that is needed is a series resistor; its purpose is to reduce the current that will flow through the LED in order to protect both the LED and the Raspberry Pi. Not a lot of current can be supplied by the output pins, and LEDs will be damaged by too high current. Furthermore LEDs are non-linear and will not light at all if too low a voltage is applied across them. Small LEDs (particularly red, yellow and green ones) will work fine with the Raspberry Pi outputs provided a resistor is attached in series. The exact resistance doesn’t matter; anything between 100 ohms and 1kohm should allow for sufficient output brightness when the output is set to logic ‘1’.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;As a quick exercise the photo here (click on it to zoom in) shows the Raspberry Pi connected up to an LED using &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-ZyyUWeY4-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=2452749&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=2452749&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;jumper wires&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-ZyyUWeY4-unlinked"&gt;jumper wires&lt;/span&gt;&lt;/span&gt;. To prevent accidental shorts and for convenience the Raspberry Pi was bolted onto a piece of wood (2mm or 2.5mm diameter bolts; 3mm will not fit) and a bread board glued next to it.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/704x449/__key/communityserver-wikis-components-files/00-00-00-01-66/0246.contentimage_5F00_199723.jpg"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/0246.contentimage_199723.jpg-704x449.jpg?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=GIl3sSrvORa8H%2F6zMcjvGVdO6winYr%2BAiNSgYpTzFK8%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=9qHBSzZEuTidXjAsc0s6lw==" style="max-height: 449px;max-width: 704px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;A bread board, a single resistor and an LED were connected up to the Raspberry Pi. The GND (also known as 0V) connection was taken from pin 6 on the Raspberry Pi header, and the GPIO pin used was GPIO22 (see the diagram earlier) which is pin 15. Any GPIO pin could be used; they are marked in pink on the earlier diagram. Another way of representing this is shown in the schematic below.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/452x269/__key/communityserver-wikis-components-files/00-00-00-01-66/4478.contentimage_5F00_199724.png"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/4478.contentimage_199724.png-452x268.png?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=PikKQx%2B8n4F6shZD%2BW6xvvyLuYd1vX7rkxZec3ywoH8%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=3pg/toO+1BrUmWD8IH01TA==" style="max-height: 268px;max-width: 452px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Once you’ve connected up the LED, a program or script can be written to control it. The choice of language is up to you. One (popular) example is Python; here is some code that will light up the LED.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;pre class="ui-code" data-mode="python"&gt;# Connect LED to GPIO22 (pin 15)
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT)
GPIO.output(22, True)
time.sleep(3)
GPIO.cleanup()

     &lt;/pre&gt;&lt;/p&gt;&lt;div style="display:none;"&gt;&lt;/div&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Save the code to a file called &lt;strong&gt;led-test.py&lt;/strong&gt; and then to run it, type&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;sudo python led-test.py&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Here is another example in Python. It flashes the LED 10 times (Note: if you&amp;#39;re new to Python, unlike other programming languages the line indentations in the code are important):&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;pre class="ui-code" data-mode="python"&gt;# connect LED to GPIO22 (pin 15)
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
for x in range(0,10):
  GPIO.setup(22, GPIO.OUT)
  GPIO.output(22, True)
  time.sleep(0.05)
  GPIO.output(22, False)
  time.sleep(0.45)

GPIO.cleanup()
     &lt;/pre&gt;&lt;/p&gt;&lt;div style="display:none;"&gt;&lt;/div&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Another way to control the output pins is to use a shell script. This appears slightly more complex (unless you enjoy writing shell scripts) but is a good thing just for reference because often other programming languages allow you to run shell scripts, and therefore if needed this can be a quick approach to using GPIO with other languages. Furthermore the method described here is standard across different platforms so your code can be ported to boards other than the Raspberry Pi too. If you&amp;#39;re a beginner to programming, stick with Python for now and move on to the next example; shell scripts can be revisited at a much later stage.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#!/bin/sh
GPIO_PATH=/sys/class/gpio
LED_PIN=22 #GPIO 22 is pin 15
echo &amp;quot;$LED_PIN&amp;quot; &amp;gt; $GPIO_PATH/export
echo &amp;quot;out&amp;quot; &amp;gt; $GPIO_PATH/gpio$LED_PIN/direction
echo &amp;quot;1&amp;quot; &amp;gt; $GPIO_PATH/gpio$LED_PIN/value
sleep 1
echo &amp;quot;$LED_PIN&amp;quot; &amp;gt; $GPIO_PATH/unexport
     &lt;/pre&gt;&lt;/p&gt;&lt;div style="display:none;"&gt;&lt;/div&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The first line of the shell script looks like a comment line, but is needed to make it easy for the Linux shell to know what to do with the script upon execution, so don’t modify that. The remainder script lines are used to take control of a GPIO pin, set it to become an output, set it to a logic high value, and then wait for 1 second. Finally control of the GPIO pin is released so other programs can make use of it if desired.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Another programming language that may be encountered is C or its (far) bigger brother C++. An example using C or C++ is given further below in the ‘Voltmeter’ section where the code is used for input from a circuit, and output to an LED.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;2) Output Example: Tone Generator&lt;/td&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;&lt;h1&gt;Another Output Example: Tone Generator&lt;/h1&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;A more interesting example than an LED is to use a couple of GPIO pins to control a tone generator circuit. The Raspberry Pi has a headphone connector for audio but sometimes just a simple tone or alarm is needed to signal events (such as for an alarm clock project).&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The photo below shows a very simple single integrated circuit (IC) project that can generate various sounds. By adjusting resistor and capacitor values, different tones and sounds could be created.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/726x485/__key/communityserver-wikis-components-files/00-00-00-01-66/3073.contentimage_5F00_199725.jpg"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/3073.contentimage_199725.jpg-726x484.jpg?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=3jKtD8WS3hDXoKPYKHhzikVcHuR%2Fu9duN%2BBaTiFtvDA%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=6YvkvP19z63MDkc0Q5qEmw==" style="max-height: 484px;max-width: 726px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The circuit connects to two outputs from the Raspberry Pi. One output is used to switch the tone on or off, and the other is used to alternate between two tones. One of the capacitors in the circuit is used to create a sweeping tone transition when tones are alternated. The circuit diagram is shown below; it uses an &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-F1phHYdR-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=9663762&amp;amp;nsku=57K3903&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=9663762&amp;amp;nsku=57K3903&amp;amp;COM=noscript" target="_blank"&gt;ICM7555 timer integrated circuit&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-F1phHYdR-unlinked"&gt;ICM7555 timer integrated circuit&lt;/span&gt;&lt;/span&gt;. GPIO22 was used to switch the tone on and off, and GPIO27 modifies the tone. C1, R1 and R2 can be adjusted to select the desired tones.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/720x315/__key/communityserver-wikis-components-files/00-00-00-01-66/3482.contentimage_5F00_199726.png"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/3482.contentimage_199726.png-720x314.png?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=p06P9dAaqGZ7hl5A8d6X0clPOplf%2BdcRuz7tTfSl6vA%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=V9Y8isgHDqImwNtsqJzHug==" style="max-height: 314px;max-width: 720px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The Python code below plays some alarm tone effects. To use the code, save the code to a file called &lt;strong&gt;tone-test.py&lt;/strong&gt; and then type&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;sudo python tone-test.py&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;pre class="ui-code" data-mode="python"&gt;import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT)  # TONE_ENABLE
GPIO.setup(27, GPIO.OUT)  # TONE_CHANGE


GPIO.output(22, True)
for x in range(0,5):
  GPIO.output(27, True)
  time.sleep(0.1)
  GPIO.output(27, False)
  time.sleep(0.2)
GPIO.output(22, False)
time.sleep(0.5)


for x in range(0,5):
  GPIO.output(22, True)
  time.sleep(0.1)
  GPIO.output(22, False)
  time.sleep(0.2)
GPIO.output(27, True)
time.sleep(0.2)


for x in range(0,5):
  GPIO.output(22, True)
  time.sleep(0.05)
  GPIO.output(22, False)
  time.sleep(0.05)


for x in range(0,10):
  GPIO.output(22, True)
  time.sleep(0.1)
  GPIO.output(22, False)
  time.sleep(0.1)
time.sleep(1)


GPIO.cleanup()
     &lt;/pre&gt;&lt;/p&gt;&lt;div style="display:none;"&gt;&lt;/div&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;line-height:1.5em;"&gt;Note that the circuit uses the 3.3V supply (pin 1) available from the Raspberry Pi. Only use this supply if you know your circuit doesn’t consume much power. More than a few tens of milliamps of current consumption would require your own external power supply. Another approach would be to build a 5V to 3.3V regulator circuit and connect it to the 5V supply available on pin 2 from the Raspberry Pi.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border:1px solid black;border:1px solid #000000;"&gt;3) Output Design Tips&lt;/td&gt;&lt;td style="border:1px solid black;border:1px solid #000000;"&gt;&lt;h1&gt;Output Design Tips&lt;/h1&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;All the circuits described so far have involved low current and low voltage but sometimes higher power devices need to be controlled or monitored. There are some typical methods to achieve this, and they are described here.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;h3&gt;Connecting to 5V Logic&lt;/h3&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;When the Raspberry Pi is used to &lt;strong&gt;output&lt;/strong&gt; a signal to 5V devices, usually (not always) no additional circuit is needed; many 5V logic device inputs will function just fine with the 3.3V logic output signals from the Raspberry Pi. You can confirm by checking the datasheet for the 5V device.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;h3&gt;Low Power, Low Voltage LEDs (red, yellow, green)&lt;/h3&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;A single LED can be controlled using a series resistor between around 100 ohms and 1kohm. No other considerations are required.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;h3&gt;Any Color Low Power LED&lt;/h3&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Blue and white LEDs may require a voltage higher than 3.3V. The easiest way to control a single LED is to use a transistor circuit to switch the 5V supply rail. The value of R1 should be increased if using an LED of any color other than blue or white. In general consult the LED data sheet. Any NPN transistor should work for this circuit.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/456x347/__key/communityserver-wikis-components-files/00-00-00-01-66/2806.contentimage_5F00_199727.png"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/2806.contentimage_199727.png-456x346.png?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=NArRz6DEAANc2J%2Bg3DGQpxOxGDUzQO0RptN2tto2zC8%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=pcm4IktspalxA0L6bRytEQ==" style="max-height: 346px;max-width: 456px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;h3&gt;Higher Power AC or DC Devices (but not mains!)&lt;/h3&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;A relay is useful for this scenario. Virtually any NPN transistor can be used. Popular devices include &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-wUaYsvj9-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=9558560&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=9558560&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;BC547B&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-wUaYsvj9-unlinked"&gt;BC547B&lt;/span&gt;&lt;/span&gt;, &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-MVhDQgca-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=9846743&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=9846743&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;2N3904&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-MVhDQgca-unlinked"&gt;2N3904&lt;/span&gt;&lt;/span&gt; and &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-reqoyDua-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=2453797&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=2453797&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;BC549&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-reqoyDua-unlinked"&gt;BC549&lt;/span&gt;&lt;/span&gt;. If the connected device can function from 5V DC then the Raspberry Pi&amp;#39;s 5V connection on pin 2 of the 40-pin header could be connected to the relay switch as long as the required current is not too high (use 100mA as a very conservative guideline) otherwise the relay switch can be connected to an external supply (take care it doesn&amp;#39;t accidentally short to the 5V supply or any other connection on the Raspberry Pi - keep it totally isolated). Check the relay datasheet to confirm that it is suitable for your needs.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;A small relay with pins convenient for breadboard layout is Finder &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-XtQASCmE-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=1329651&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=1329651&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;32.21.7.005.2000&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-XtQASCmE-unlinked"&gt;32.21.7.005.2000&lt;/span&gt;&lt;/span&gt; and it can switch up to 6A. Note that just because a relay is rated for (say) 250V does not mean it can be used for this. Breadboards and stripboards are designed for far lower voltages. Even with a custom PCB the project should not be used at mains voltages because there are legal requirements for conductor spacing, temperature, types of enclosure material, connection type, conductor clamping method and so on. It is easy to create a product that will fail and cause personal injury or an electrical fire or harm others at some point in time.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/494x347/__key/communityserver-wikis-components-files/00-00-00-01-66/7776.contentimage_5F00_199728.png"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/7776.contentimage_199728.png-494x346.png?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=fTwOWCrFIAkJb%2BULR4PbrjgMJ1igZm3rBOOdBDLPtCM%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=jPRVMq69ph/7n2N9UorWmQ==" style="max-height: 346px;max-width: 494px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;h3&gt;Mains Powered Devices&lt;/h3&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Treat any circuit or product that directly controls mains devices from a Raspberry Pi with extreme suspicion. Most do not meet safety standards (even though some firms self-certify that they do meet safety standards when clearly their products do not). Instead, a reasonably safe method is to find a reputable remote control product from a reputable manufacturer and a reputable supplier that uses infra-red or wireless (radio) technology to control the mains device. In the UK, the &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-vLx122nb-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=2433438&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=2433438&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;Energenie RF controlled mains sockets&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-vLx122nb-unlinked"&gt;Energenie RF controlled mains sockets&lt;/span&gt;&lt;/span&gt; appear suitable and they provide a small radio transmitter board that directly plugs onto the Raspberry Pi 40-pin connector and they also supply example Python code for it. For an examination of the hardware and software click to see &lt;a class="jive-link-external-small" href="http://www.element14.com/community/community/designcenter/single-board-computers/blog/2014/11/19/energenie-experiments-remote-power-control-for-the-home" target="_blank"&gt;Energenie Experiments - Remote Power Control for the Home&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Some products for non-UK regions are mentioned at that link too.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;h3&gt;Multiple Relays, LEDs or Devices that require less than 200mA Current at 12V&lt;/h3&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The ancient (&amp;gt;25 years in production!) yet useful &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-Iiu285cs-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=1094428&amp;amp;nsku=89K1143&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=1094428&amp;amp;nsku=89K1143&amp;amp;COM=noscript" target="_blank"&gt;ULN2803A&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-Iiu285cs-unlinked"&gt;ULN2803A&lt;/span&gt;&lt;/span&gt; integrated circuit can be employed for this scenario. Several hundred milliamps can be supplied by each output pin (there are 8 of them).&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/806x371/__key/communityserver-wikis-components-files/00-00-00-01-66/2818.contentimage_5F00_199729.png"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/2818.contentimage_199729.png-806x370.png?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=J%2BxxPcTrXMaVrZ17KsTXhuZC6drEWn0mspGABO8qU2M%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=2A35t3kGlVYHldALVHpAvQ==" style="max-height: 370px;max-width: 806px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;h3&gt;Driving small DC Motors (the most common motors, also known as ‘Brushed DC’ Motors) and Stepper Motors&lt;/h3&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;For small motors consider the &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-xAXvsvcm-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=2452189&amp;amp;nsku=93X7452&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=2452189&amp;amp;nsku=93X7452&amp;amp;COM=noscript" target="_blank"&gt;Gertbot motor control board&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-xAXvsvcm-unlinked"&gt;Gertbot motor control board&lt;/span&gt;&lt;/span&gt;. It can drive several brushed DC motors if needed, and can also be used for stepper motors.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;h3&gt;Driving Brushed DC Motors (Medium to High Power) in either Direction&lt;/h3&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Use the&lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-eC5JTWXw-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=2453485&amp;amp;nsku=93X8231&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=2453485&amp;amp;nsku=93X8231&amp;amp;COM=noscript" target="_blank"&gt;&amp;nbsp; Infineon Motor Control board&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-eC5JTWXw-unlinked"&gt;&amp;nbsp; Infineon Motor Control board&lt;/span&gt;&lt;/span&gt;. It can control one motor in both directions, or two motors in one direction. If more motors are needed, add more of the boards.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h1&gt;&lt;/h1&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;h1&gt;Understanding Raspberry Pi Inputs&lt;/h1&gt;&lt;p style="margin:0;"&gt;&lt;span style="font-size:12pt;"&gt;Work through the three tabs below to get a full understanding of the input functions of your Raspberry Pi.&lt;/span&gt;&lt;/p&gt;&lt;table border="1" class="jiveBorder" style="border:1px solid #000000;width:100%;"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style="border:1px solid black;border:1px solid #000000;padding:2px;color:#ffffff;background-color:#6690bc;text-align:center;" valign="middle"&gt;&lt;strong&gt;{tabbedtable} Tab Label&lt;/strong&gt;&lt;/th&gt;&lt;th style="border:1px solid black;border:1px solid #000000;padding:2px;color:#ffffff;background-color:#6690bc;text-align:center;" valign="middle"&gt;&lt;strong&gt;Tab Content&lt;/strong&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;1) Push Button Switch Input&lt;/td&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;&lt;h1&gt;Push Button Switch Input&lt;/h1&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The connections on the Raspberry Pi can be converted into inputs under software control. The example here is about the simplest; a push button switch is wired up to the 40-pin connector:&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/584x595/__key/communityserver-wikis-components-files/00-00-00-01-66/7608.contentimage_5F00_199730.jpg"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/7608.contentimage_199730.jpg-584x594.jpg?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=XDR7rpxkc8uVZiDFPzi%2FX4OGtqKVQjHE2EUlf0PG9k0%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=JW/X6bBV4V/2QzhuZJt1/w==" style="max-height: 594px;max-width: 584px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The circuit diagram is shown below. Note that the LED circuit from the exercises before is still left wired up but is not shown in the diagram below.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/402x340/__key/communityserver-wikis-components-files/00-00-00-01-66/6204.contentimage_5F00_199731.jpg"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/6204.contentimage_199731.jpg-402x339.jpg?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=tgvfEIo19P%2F5oYqFX8xlBrurSYaygvbCxHCviNiFmzY%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=W9KpDDBPMrxBhSDMmIDImg==" style="max-height: 339px;max-width: 402px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;As can be seen from the circuit diagram, when the switch is not pressed the input pin is connected to +3.3V through a resistor. The input pin has a high resistance inside the Raspberry Pi, and so the voltage at the input pin is close to the 3.3V that the resistor is connected to. This means that the Raspberry Pi will detect a logic ‘1’ condition when the switch is not pressed.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;When the switch is pressed, there is a direct path between the input pin and 0V and so the voltage at the input pin becomes 0V which is a logic ‘0’ condition.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;For this simple switch circuit, the value of resistor R1 is not critical and can be any resistance from around 1k to 47k, but avoid low values since that increases current consumption when the switch is pressed.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Here is some example Python code that can detect the push button switch status. To use the code, save the code in a file called &lt;strong&gt;button-test.py&lt;/strong&gt; and then to run it type:&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;sudo python button-test.py&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;pre class="ui-code" data-mode="python"&gt;# Connections:
# GPIO5 (pin 29) is button input
# GPIO22 (pin 15) is LED


import time
import curses
import sys
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN)
GPIO.setup(22, GPIO.OUT)
tc=curses.initscr()
tc.nodelay(1)
old_button_status=GPIO.input(5)
count=0


tc.addstr(1, 0, &amp;quot;Press SPACE to quit:\n&amp;quot;)
tc.addstr(2, 0, str(count))


while 1:
  button_status=GPIO.input(5)
  kbval=tc.getch()
  GPIO.output(22, button_status)
  if old_button_status != button_status:
    if button_status == True:
      tc.addstr(2, 10, &amp;quot;-&amp;quot;) # pin is high, button unpressed
      count=count+1
    else:
      tc.addstr(2, 10, &amp;quot;_&amp;quot;) # pin is low, button pressed
    old_button_status=button_status
    tc.addstr(2, 0, str(count))
  if kbval==0x20:
    break


time.sleep(1)
GPIO.cleanup()
curses.endwin()
print &amp;quot;Goodbye&amp;quot;

     &lt;/pre&gt;&lt;/p&gt;&lt;div style="display:none;"&gt;&lt;/div&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;line-height:1.5em;"&gt;The code displays the button status on the screen, along with a count of how many times the button was pressed. You’ll notice that occasionally the count increases more than the number of times the button was thought to be pressed and this happens because of ‘contact bounce’; as the switch is pressed, the contacts connect together and release many times rapidly before finally making contact. A similar thing happens when the switch is released. The solution is to have ‘debouncing’ functionality. It can be implemented in hardware or in software (the software method is very common). The code below implements debouncing and will more accurately register the correct number of button presses. The only difference with the earlier code is that this time the input status is checked a second time delayed by about 20msec – this provides time for contact bounce to have completed.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;pre class="ui-code" data-mode="python"&gt;# Connections:
# GPIO5 (pin 29) is button input
# GPIO22 (pin 15) is LED


import time
import curses
import sys
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN)
GPIO.setup(22, GPIO.OUT)
tc=curses.initscr()
tc.nodelay(1)
old_button_status=GPIO.input(5)
count=0


tc.addstr(1, 0, &amp;quot;Press SPACE to quit:\n&amp;quot;)
tc.addstr(2, 0, str(count))


while 1:
  button_status=GPIO.input(5)
  kbval=tc.getch()
  GPIO.output(22, button_status)
  if old_button_status != button_status:
    time.sleep(0.02) # debounce period
    button_status=GPIO.input(5) # re-read the input
    if old_button_status != button_status:


      if button_status == True:
        tc.addstr(2, 10, &amp;quot;-&amp;quot;) # pin is high, button unpressed
        count=count+1
      else:
        tc.addstr(2, 10, &amp;quot;_&amp;quot;) # pin is low, button pressed
      old_button_status=button_status
      tc.addstr(2, 0, str(count))


  if kbval==0x20:
    break


time.sleep(1)
GPIO.cleanup()
curses.endwin()
print &amp;quot;Goodbye&amp;quot;

     &lt;/pre&gt;&lt;/p&gt;&lt;div style="display:none;"&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;2) Input Example: Voltmeter&lt;/td&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;&lt;h1&gt;Another Input Example: A Voltmeter&lt;/h1&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The Raspberry Pi has digital inputs and outputs but the real world has analog quantities that may need to be measured or controlled. The example here converts analog values into a digital signal that can be read using a Raspberry Pi input connection.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The example is an extremely simple voltmeter; it can measure voltages between 0 and 1V with good precision. Note that this won’t replace a handheld multimeter! It doesn’t have the requisite safety protection and could damage itself or persons or property if used for anything beyond the scope of the examples described here.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/826x465/__key/communityserver-wikis-components-files/00-00-00-01-66/2084.contentimage_5F00_199732.jpg"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/2084.contentimage_199732.jpg-826x464.jpg?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=GU7ulgy%2BzJBOSfrOK%2BCgqYrGeOfhE2t0dFVAzl%2FdVbA%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=xxG5hu7A4nnySqpTQKXL/w==" style="max-height: 464px;max-width: 826px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;This is the circuit diagram:&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/870x282/__key/communityserver-wikis-components-files/00-00-00-01-66/5226.contentimage_5F00_199733.png"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/5226.contentimage_199733.png-870x282.png?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=IWFm3xvTu40iidUQaw1IpyQuvF0gS%2BIY7h6csxwCX%2FE%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=9Ovci+rP6ZYqP8ikiCS8HQ==" style="max-height: 282px;max-width: 870px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The circuit uses an &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-Sp9qSXwH-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=9605053&amp;amp;nsku=59K4465&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=9605053&amp;amp;nsku=59K4465&amp;amp;COM=noscript" target="_blank"&gt;AD654 integrated circuit&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-Sp9qSXwH-unlinked"&gt;AD654 integrated circuit&lt;/span&gt;&lt;/span&gt; which generates a square wave with frequency dependant on an input voltage. The square wave output is connected to a Raspberry Pi input (GPIO5 in this example). The square wave has a frequency of up to 10kHz and so some accurate timing is needed to determine the voltmeter input. The code here uses the C programming language and a library called &lt;strong&gt;wiringpi&lt;/strong&gt; written by Gordon Henderson. First off, install the wiringpi library:&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;mkdir development
cd development
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build
     &lt;/pre&gt;&lt;/p&gt;&lt;div style="display:none;"&gt;&lt;/div&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Next, save the following code into a file called &lt;strong&gt;voltmeter.c&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// AD654 Voltmeter
// Ver 1.0 Shabaz August 2015
// GPIO5 (pin 29) connects to AD654
// GPIO22 (pin 15) connects to LED


#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;time.h&amp;gt;
#include &amp;lt;wiringPi.h&amp;gt;

#define INREF 5
#define LEDREF 22
#define INVAL (digitalRead(INREF))
#define INVAL_IS_HIGH (digitalRead(INREF)==1)
#define INVAL_IS_LOW (digitalRead(INREF)==0)
#define LED_ON digitalWrite(LEDREF, 1)
#define LED_OFF digitalWrite(LEDREF, 0)


struct timespec ts_zero;


void
get_tzero(void)
{
  clock_gettime(CLOCK_MONOTONIC, &amp;amp;ts_zero);
}


long
get_time(void)
{
  long nsec, sec;
  struct timespec tts;
  clock_gettime(CLOCK_MONOTONIC, &amp;amp;tts);
  sec=tts.tv_sec-ts_zero.tv_sec;
  nsec=tts.tv_nsec-ts_zero.tv_nsec;
  if (sec&amp;gt;0)
  {
    nsec=nsec+1E9; // we don&amp;#39;t expect times greater than 1 sec
  }
  ts_zero=tts;
  return(nsec);
}


int
main(int argc, char *argv[])
{
  int i;
  long avg;
  double mv, v;
  long nsec[65];
  wiringPiSetupGpio();
  pinMode(INREF, INPUT);
  pinMode(LEDREF, OUTPUT);


  while(1)
  {
    LED_ON;
    while(INVAL_IS_LOW);
    get_tzero();
    for (i=0; i&amp;lt;64; i++)
    {
      while(INVAL_IS_HIGH);
      nsec[i]=get_time();
      while(INVAL_IS_LOW);
    }
    avg=0;
    for(i=1; i&amp;lt;65; i++)
    {
      avg=avg+nsec[i];
    }
    avg=avg/64;
    v=1.0E5/((double)avg);
    mv=v*1000;
    printf(&amp;quot;Voltage is %fV\n&amp;quot;, v);
    LED_OFF;
    sleep(1);
  }


  return(0);
}
     &lt;/pre&gt;&lt;/p&gt;&lt;div style="display:none;"&gt;&lt;/div&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;line-height:1.5em;"&gt;The code can be compiled by typing:&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;gcc -o voltmeter -lrt -lwiringPi voltmeter.c&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;To run the code, type:&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;sudo ./voltmeter&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The code uses an averaging method to get high precision. It rapidly measures the width of the incoming square wave 64 times, and then sums the values and divides by 64 to get the average value. Then it sleeps for a second to allow other processes to do their thing, and then repeats.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The circuit also needs a few things for high precision. The &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-zkotaja0-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=1890179&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=1890179&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;10nF timing capacitor&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-zkotaja0-unlinked"&gt;10nF timing capacitor&lt;/span&gt;&lt;/span&gt; for the AD564 is selected to be a polypropylene part for better accuracy. The &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-gnc3LmOx-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=9341102&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=9341102&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;1k timing resistor&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-gnc3LmOx-unlinked"&gt;1k timing resistor&lt;/span&gt;&lt;/span&gt; can be a 1% part or alternatively a variable trimming resistor can be used. The circuit runs from the 5V supply from the Raspberry Pi 40-pin connector but this is only approximately 5V and so the circuit would benefit from a dedicated and more accurate 5V supply circuit.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;As a quick check, the circuit shown above had the voltmeter input subjected to a potential divider circuit consisting of two resistors. The calculated voltage (if the resistors were precisely the correct values and the supply was exactly 5V) out of the potential divider is 0.04950V. When the software was run, it reported measurements of 0.04991V in the short test. Not bad for such a simple project!&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Diodes D1 and D2 in the circuit above are optional but highly recommended because they will provide some protection to the integrated circuit if the input is accidentally subjected to a voltage outside the 0-1V range.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border:1px solid black;border:1px solid #000000;"&gt;3) Input Design Tips&lt;/td&gt;&lt;td style="border:1px solid black;border:1px solid #000000;"&gt;&lt;h1&gt;Input Design Tips&lt;/h1&gt;&lt;h3&gt;&lt;/h3&gt;&lt;h3&gt;Protected Inputs&lt;/h3&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The Raspberry Pi can be protected from accidental connections to higher than expected voltages by adding an additional circuit to the inputs in use. A very good option in some situations is to use an optocoupler alternatively a low-cost method such as the circuit below will tolerate voltages as high as 60V without damaging the Raspberry Pi, and will cope with reversed connections too. The &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-SZMSf55o-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=9558560&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=9558560&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;BC547B&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-SZMSf55o-unlinked"&gt;BC547B&lt;/span&gt;&lt;/span&gt; transistor in the circuit can be replaced with another type such as &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-XlZRfBWL-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=9846743&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=9846743&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;2N3904&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-XlZRfBWL-unlinked"&gt;2N3904&lt;/span&gt;&lt;/span&gt; or &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-adKS48Xh-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=2453797&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=2453797&amp;amp;nsku=NULL&amp;amp;COM=noscript" target="_blank"&gt;BC549&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-adKS48Xh-unlinked"&gt;BC549&lt;/span&gt;&lt;/span&gt; if more convenient to obtain. R1 can be a (normal) 0.25W resistor for inputs up to around 50V, and will need to be a 0.5W part otherwise.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The output of this circuit is inverted, and an input voltage higher than about 2V will pull the output low.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/730x317/__key/communityserver-wikis-components-files/00-00-00-01-66/5621.contentimage_5F00_199734.png"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/5621.contentimage_199734.png-730x316.png?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=vY7PTCOIFPytXgWJSk9L14QjtmJ9z0eRHwqkz3UGIKg%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=gt9vucrqzk+t4hIl/Lyc9w==" style="max-height: 316px;max-width: 730px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;h3&gt;Connecting to 5V Logic&lt;/h3&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;A direct connection of a 5V logic output to a Raspberry Pi input could cause damage. Different approaches can be taken depending on the specific need.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;If the 5V logic output changes slowly then you could consider using a potential divider circuit (i.e. two resistors) but it is not a great method (it doesn’t work for fast circuits). A far better method is to use a 5V tolerant buffer integrated circuit. A mid-way approach adequate for up to 100kHz switching speed is shown below, using a &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-cXScKAVH-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=9525440&amp;amp;nsku=98K4100&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=9525440&amp;amp;nsku=98K4100&amp;amp;COM=noscript" target="_blank"&gt;ZVN2110A&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-cXScKAVH-unlinked"&gt;ZVN2110A&lt;/span&gt;&lt;/span&gt; N-channel MOSFET. The output is inverted.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/460x335/__key/communityserver-wikis-components-files/00-00-00-01-66/2577.contentimage_5F00_199735.png"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/2577.contentimage_199735.png-460x334.png?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=XJLbwlO1UJbslS6PvbxNgAeJucAnGuK%2BOUGUxDabSCo%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=rACWUFbm2uyhhPGkZf4ttw==" style="max-height: 334px;max-width: 460px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Alternative MOSFETs that are suitable include &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-aC8rJc02-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=9525483&amp;amp;nsku=98K4098&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=9525483&amp;amp;nsku=98K4098&amp;amp;COM=noscript" target="_blank"&gt;ZVNL120A&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-aC8rJc02-unlinked"&gt;ZVNL120A&lt;/span&gt;&lt;/span&gt; and &lt;span&gt;&lt;span class="e14-init-shown" id="addProduct-R0RCNUpp-linked" style="white-space:nowrap;"&gt;&lt;a class="jive-link-product-addtolist" href="https://www.element14.com/community/view-product.jspa?fsku=9525416&amp;amp;nsku=98K2375&amp;amp;COM=noscript" target="_blank"&gt;&lt;span class="pf-widget-map pf-productlink-cart-icon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class="jive-link-product pf-embedded-product-link" href="https://www.element14.com/community/view-product.jspa?fsku=9525416&amp;amp;nsku=98K2375&amp;amp;COM=noscript" target="_blank"&gt;VN10LP&lt;/a&gt;&lt;/span&gt;&lt;span class="e14-init-hidden" id="addProduct-R0RCNUpp-unlinked"&gt;VN10LP&lt;/span&gt;&lt;/span&gt;.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;MOSFETs are easy to damage when unsoldered in a circuit due to a very delicate gate structure (it is made of a thin layer internally that can be destroyed with high voltage). Take electrostatic precautions and another good practice is to first connect R3 into your circuit (either on a circuit board or breadboard) and then remove the MOSFET from its safe packaging and connect it into the circuit. The resistor R3 will provide some protection.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h1&gt;&lt;/h1&gt;&lt;h1&gt;&lt;/h1&gt;&lt;h1&gt;Additional Raspberry Pi Input &amp;amp; Output Info&lt;/h1&gt;&lt;p style="margin:0;"&gt;&lt;span style="font-size:12pt;"&gt;Check out the three tabs below for a more advanced exercise using the input and output functions, and to see where you can go next with your Raspberry Pi.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;table border="1" class="jiveBorder" style="border:1px solid #000000;width:100%;"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style="border:1px solid black;border:1px solid #000000;padding:2px;color:#ffffff;background-color:#6690bc;text-align:center;" valign="middle"&gt;&lt;strong&gt;{tabbedtable} Tab Label&lt;/strong&gt;&lt;/th&gt;&lt;th style="border:1px solid black;border:1px solid #000000;padding:2px;color:#ffffff;background-color:#6690bc;text-align:center;" valign="middle"&gt;&lt;strong&gt;Tab Content&lt;/strong&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;Advanced: Speed and Jitter&lt;/td&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;&lt;h1&gt;Advanced Topic: Speed and Jitter&lt;/h1&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Beginners: Skip to the next section! Advanced users, read on.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Button-presses and LED outputs are slow events measured in tens or hundreds of milliseconds. Sometimes much higher speed signals are encountered. For example, a TV remote emits infra-red light pulses around 40k times per second. Although the core CPU in the Raspberry Pi runs at speeds approaching 1GHz, input and output signals at that rate are not supported for several reasons that are hardware, electrical and software related. We will consider some of the software factors for the scope of this blog post.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;The Raspberry Pi runs Linux which is a multi-tasking OS. It can (at any time) pre-emptively take control away from your software program in order to service other tasks. It all happens relatively quickly, so it looks like the mouse still works while your program is running, but really the Linux OS is providing short time-slices to your program and to the mouse driver code.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Ordinarily this doesn’t matter, but when you need short or high precision events this can become an issue revealed (for example) as jitter.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Furthermore choice of programming language has an effect too, because some libraries are more suitable than others. Interpreted code may run at a different speed compared to compiled code. In short if very precise timing is needed then either a Linux driver needs to be written or external hardware (such as another microcontroller, or logic circuits and clock) may be needed.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;One good question for more advanced use of the Raspberry Pi capabilities is approximately how fast can output signals be toggled using the Python and C libraries and the script method described earlier?&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;To find out, code was written to repeatedly toggle an output pin and an oscilloscope was connected up. The results are shown below.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/1489x384/__key/communityserver-wikis-components-files/00-00-00-01-66/6710.contentimage_5F00_199736.png"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/6710.contentimage_199736.png-620x160.png?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=7t%2FtdRtYuclkdPlWN4dwwqRvo7IxQ3yuLkWkwHU%2Fp0c%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=bcsZTkSTfoYlX+1nPeUqtA==" style="max-height: 160px;max-width: 620px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;However, it is always important to remember there is jitter that will be present due to the use of the Linux OS. This is not an issue for general LED toggling of course. The oscilloscope capture below shows part of the nature of the jitter by overlaying multiple traces triggered at the same time. Notice that the jitter can take on many values, although they are all discretely spaced 4nsec apart (250MHz) which is due to internal hardware inside the Raspberry Pi.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span&gt;&lt;a href="https://community.element14.com/resized-image/__size/850x453/__key/communityserver-wikis-components-files/00-00-00-01-66/2500.contentimage_5F00_199737.png"&gt;&lt;img loading="lazy" alt="image" src="https://community-storage.element14.com/communityserver-components-secureimagefileviewer/communityserver/wikis/components/files/00/00/00/01/66/2500.contentimage_199737.png-850x452.png?sv=2016-05-31&amp;amp;sr=b&amp;amp;sig=24W1frdFtB6%2FjbxwaAvnojPzvN2Fg1sEqMMqRYfEHDc%3D&amp;amp;se=2026-04-11T23%3A59%3A59Z&amp;amp;sp=r&amp;amp;_=nEKl0qrv7iqw4phm4cVk1g==" style="max-height: 452px;max-width: 850px;" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;Summary&lt;/td&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;&lt;h1&gt;Summary&lt;/h1&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;If you&amp;#39;ve read this far then by now it should be clear that the 40-pin connector on the Raspberry Pi can be used for many diverse projects from driving LEDs and relays to creating voltmeters or generating alarms. With little circuitry the 3.3V logic compatible Raspberry Pi connections can interface to circuits. Using Python it is very easy to control outputs, and reading inputs is not too difficult either. For C programmers the wiringpi library makes control easy.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;Next Steps&lt;/td&gt;&lt;td style="border:1px solid black;border:1px solid #000000;padding:2px;"&gt;&lt;h1&gt;Next Steps&lt;/h1&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;If you have a few basic components (an LED, some resistors and a breadboard and jumper wires) you can begin exploring.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;Rather than just buy a single resistor or capacitor as you need it, consider purchasing a small pack of resistors, capacitors and a few transistors (bipolar and MOSFET), diodes, and a multimeter.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;font-size:13.3333px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;font-size:13.3333px;"&gt;&lt;span style="font-size:12pt;"&gt;As you dive into deeper projects you&amp;#39;ll also be interested in learning about the serial interface connections available on the 40-pin connector. The serial interfaces are used to send and receive multiple bits of data in a standard method between the Raspberry Pi and other integrated circuits. However there are also many integrated circuits that are very easy to connect to the GPIO connections and several were covered in this blog post. Consider obtaining a few ICM7555 devices - there are a lot of fun and interesting experiments possible with them.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;script&gt;window.top.e14.func.queueScripts.add(function() { window.top.e14.func.e14DynaloadTabbedTables.init(); });&lt;/script&gt;

&lt;div style="font-size: 90%;"&gt;Tags: gpio, rpibeginner, raspberry_pi, raspberry_pi_2, raspberry_pi_projects, guide, rpi_2&lt;/div&gt;
</description></item></channel></rss>