Silicon ChipInterfacing with the Raspberry Pi – for Beginners - December 2017 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Publisher's Letter: Australia's strength in manufacturing
  4. Feature: Rail Guns and Electromagnetic Launchers by Dr David Maddison
  5. Project: Touchscreen Altimeter and Weather station by Jim Rowe
  6. Feature: Interfacing with the Raspberry Pi – for Beginners by Andrew Pullin
  7. Project: The Arduino MegaBox from Altronics by Bao Smith
  8. Product Showcase
  9. Review: Music Hall mmf-1.3 Belt-Driven Turntable by Leo Simpson
  10. Serviceman's Log: Video trials and tribulations by Dave Thompson
  11. Project: Build your own Super-7 AM Radio Receiver – Part 2 by John Clarke
  12. Feature: El Cheapo Modules 11: Pressure/Temperature Sensors by Jim Rowe
  13. Subscriptions
  14. Project: Part 3: Finishing our new 6GHz+ Digital Frequency Meter by Nicholas Vinen
  15. PartShop
  16. Vintage Radio: Roberts R66 4-valve 2-band portable by Marc Chick
  17. Market Centre
  18. Notes & Errata: 50A Charger Controller, Nov16; Deluxe Touchscreen eFuse, Jul17; Universal Battery Valve Power Supply, Aug17; 3-way Active Crossover, Sept & Oct17; Kelvin the Cricket, Oct17; Circuit Notebook: Modifications to Universal Battery Valve Power Supply, Oct17; 6GHz+ Touchscreen Frequency Meter, Oct-Dec17; Super-7 AM Radio, Nov17
  19. Advertising Index
  20. Outer Back Cover

This is only a preview of the December 2017 issue of Silicon Chip.

You can view 38 of the 104 pages in the full issue, including the advertisments.

For full access, purchase the issue for $10.00 or subscribe for access to the latest issues.

Items relevant to "Touchscreen Altimeter and Weather station":
  • PIC32MX170F256B-50I/SP programmed for the Touchscreen Altimeter/Weather Station (Programmed Microcontroller, AUD $15.00)
  • GY-68 Barometric Pressure/Altitude/Temperature I²C Sensor breakout board (Component, AUD $2.50)
  • DHT22/AM2302 Compatible Temperature and Humidity sensor module (Component, AUD $9.00)
  • Elecrow 1A/500mA Li-ion/LiPo charger board with USB power pass-through (Component, AUD $25.00)
  • Micromite LCD BackPack V2 complete kit (Component, AUD $70.00)
  • Firmware (HEX) and BASIC source code for the Touchscreen Altimeter/Weather Station (Software, Free)
  • Altimeter/Weather Station front panel artwork (PDF download) (Free)
Items relevant to "The Arduino MegaBox from Altronics":
  • Firmware (Arduino sketch) for the LC Meter (Mega Box) (Software, Free)
  • Firmware (Arduino sketch) for the VS1053 Music Player (Mega Box) (Software, Free)
Articles in this series:
  • The Arduino MegaBox from Altronics (December 2017)
  • The Arduino MegaBox from Altronics (December 2017)
  • Arduino LC Meter Shield Kit (January 2018)
  • Arduino LC Meter Shield Kit (January 2018)
  • The Arduino Mega Box Music Player revisited (February 2018)
  • The Arduino Mega Box Music Player revisited (February 2018)
  • Altronics New MegaBox V2 Arduino prototyping system (December 2019)
  • Altronics New MegaBox V2 Arduino prototyping system (December 2019)
Items relevant to "Build your own Super-7 AM Radio Receiver – Part 2":
  • Super-7 AM Radio PCB [06111171] (AUD $25.00)
  • Case pieces for the Super-7 AM Radio (PCB, AUD $25.00)
Articles in this series:
  • Build the Super-7 – a single-board AM radio receiver (November 2017)
  • Build the Super-7 – a single-board AM radio receiver (November 2017)
  • Build your own Super-7 AM Radio Receiver – Part 2 (December 2017)
  • Build your own Super-7 AM Radio Receiver – Part 2 (December 2017)
Items relevant to "El Cheapo Modules 11: Pressure/Temperature Sensors":
  • GY-68 Barometric Pressure/Altitude/Temperature I²C Sensor breakout board (Component, AUD $2.50)
  • GY-BM BMP280 module (Component, AUD $5.00)
  • GY-BME280 Barometric Pressure/Altitude/Temperature/Humidity I²C Sensor breakout board (Component, AUD $12.50)
  • GY-BME280 module (6-pin/3.3V version) (Component, AUD $10.00)
  • Software for El Cheapo Modules: GY-68 (BMP180) (Free)
Articles in this series:
  • El Cheapo Modules From Asia - Part 1 (October 2016)
  • El Cheapo Modules From Asia - Part 1 (October 2016)
  • El Cheapo Modules From Asia - Part 2 (December 2016)
  • El Cheapo Modules From Asia - Part 2 (December 2016)
  • El Cheapo Modules From Asia - Part 3 (January 2017)
  • El Cheapo Modules From Asia - Part 3 (January 2017)
  • El Cheapo Modules from Asia - Part 4 (February 2017)
  • El Cheapo Modules from Asia - Part 4 (February 2017)
  • El Cheapo Modules, Part 5: LCD module with I²C (March 2017)
  • El Cheapo Modules, Part 5: LCD module with I²C (March 2017)
  • El Cheapo Modules, Part 6: Direct Digital Synthesiser (April 2017)
  • El Cheapo Modules, Part 6: Direct Digital Synthesiser (April 2017)
  • El Cheapo Modules, Part 7: LED Matrix displays (June 2017)
  • El Cheapo Modules, Part 7: LED Matrix displays (June 2017)
  • El Cheapo Modules: Li-ion & LiPo Chargers (August 2017)
  • El Cheapo Modules: Li-ion & LiPo Chargers (August 2017)
  • El Cheapo modules Part 9: AD9850 DDS module (September 2017)
  • El Cheapo modules Part 9: AD9850 DDS module (September 2017)
  • El Cheapo Modules Part 10: GPS receivers (October 2017)
  • El Cheapo Modules Part 10: GPS receivers (October 2017)
  • El Cheapo Modules 11: Pressure/Temperature Sensors (December 2017)
  • El Cheapo Modules 11: Pressure/Temperature Sensors (December 2017)
  • El Cheapo Modules 12: 2.4GHz Wireless Data Modules (January 2018)
  • El Cheapo Modules 12: 2.4GHz Wireless Data Modules (January 2018)
  • El Cheapo Modules 13: sensing motion and moisture (February 2018)
  • El Cheapo Modules 13: sensing motion and moisture (February 2018)
  • El Cheapo Modules 14: Logarithmic RF Detector (March 2018)
  • El Cheapo Modules 14: Logarithmic RF Detector (March 2018)
  • El Cheapo Modules 16: 35-4400MHz frequency generator (May 2018)
  • El Cheapo Modules 16: 35-4400MHz frequency generator (May 2018)
  • El Cheapo Modules 17: 4GHz digital attenuator (June 2018)
  • El Cheapo Modules 17: 4GHz digital attenuator (June 2018)
  • El Cheapo: 500MHz frequency counter and preamp (July 2018)
  • El Cheapo: 500MHz frequency counter and preamp (July 2018)
  • El Cheapo modules Part 19 – Arduino NFC Shield (September 2018)
  • El Cheapo modules Part 19 – Arduino NFC Shield (September 2018)
  • El cheapo modules, part 20: two tiny compass modules (November 2018)
  • El cheapo modules, part 20: two tiny compass modules (November 2018)
  • El cheapo modules, part 21: stamp-sized audio player (December 2018)
  • El cheapo modules, part 21: stamp-sized audio player (December 2018)
  • El Cheapo Modules 22: Stepper Motor Drivers (February 2019)
  • El Cheapo Modules 22: Stepper Motor Drivers (February 2019)
  • El Cheapo Modules 23: Galvanic Skin Response (March 2019)
  • El Cheapo Modules 23: Galvanic Skin Response (March 2019)
  • El Cheapo Modules: Class D amplifier modules (May 2019)
  • El Cheapo Modules: Class D amplifier modules (May 2019)
  • El Cheapo Modules: Long Range (LoRa) Transceivers (June 2019)
  • El Cheapo Modules: Long Range (LoRa) Transceivers (June 2019)
  • El Cheapo Modules: AD584 Precision Voltage References (July 2019)
  • El Cheapo Modules: AD584 Precision Voltage References (July 2019)
  • Three I-O Expanders to give you more control! (November 2019)
  • Three I-O Expanders to give you more control! (November 2019)
  • El Cheapo modules: “Intelligent” 8x8 RGB LED Matrix (January 2020)
  • El Cheapo modules: “Intelligent” 8x8 RGB LED Matrix (January 2020)
  • El Cheapo modules: 8-channel USB Logic Analyser (February 2020)
  • El Cheapo modules: 8-channel USB Logic Analyser (February 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules (May 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules (May 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules, Part 2 (June 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules, Part 2 (June 2020)
  • El Cheapo Modules: Mini Digital Volt/Amp Panel Meters (December 2020)
  • El Cheapo Modules: Mini Digital Volt/Amp Panel Meters (December 2020)
  • El Cheapo Modules: Mini Digital AC Panel Meters (January 2021)
  • El Cheapo Modules: Mini Digital AC Panel Meters (January 2021)
  • El Cheapo Modules: LCR-T4 Digital Multi-Tester (February 2021)
  • El Cheapo Modules: LCR-T4 Digital Multi-Tester (February 2021)
  • El Cheapo Modules: USB-PD chargers (July 2021)
  • El Cheapo Modules: USB-PD chargers (July 2021)
  • El Cheapo Modules: USB-PD Triggers (August 2021)
  • El Cheapo Modules: USB-PD Triggers (August 2021)
  • El Cheapo Modules: 3.8GHz Digital Attenuator (October 2021)
  • El Cheapo Modules: 3.8GHz Digital Attenuator (October 2021)
  • El Cheapo Modules: 6GHz Digital Attenuator (November 2021)
  • El Cheapo Modules: 6GHz Digital Attenuator (November 2021)
  • El Cheapo Modules: 35MHz-4.4GHz Signal Generator (December 2021)
  • El Cheapo Modules: 35MHz-4.4GHz Signal Generator (December 2021)
  • El Cheapo Modules: LTDZ Spectrum Analyser (January 2022)
  • El Cheapo Modules: LTDZ Spectrum Analyser (January 2022)
  • Low-noise HF-UHF Amplifiers (February 2022)
  • Low-noise HF-UHF Amplifiers (February 2022)
  • A Gesture Recognition Module (March 2022)
  • A Gesture Recognition Module (March 2022)
  • Air Quality Sensors (May 2022)
  • Air Quality Sensors (May 2022)
  • MOS Air Quality Sensors (June 2022)
  • MOS Air Quality Sensors (June 2022)
  • PAS CO2 Air Quality Sensor (July 2022)
  • PAS CO2 Air Quality Sensor (July 2022)
  • Particulate Matter (PM) Sensors (November 2022)
  • Particulate Matter (PM) Sensors (November 2022)
  • Heart Rate Sensor Module (February 2023)
  • Heart Rate Sensor Module (February 2023)
  • UVM-30A UV Light Sensor (May 2023)
  • UVM-30A UV Light Sensor (May 2023)
  • VL6180X Rangefinding Module (July 2023)
  • VL6180X Rangefinding Module (July 2023)
  • pH Meter Module (September 2023)
  • pH Meter Module (September 2023)
  • 1.3in Monochrome OLED Display (October 2023)
  • 1.3in Monochrome OLED Display (October 2023)
  • 16-bit precision 4-input ADC (November 2023)
  • 16-bit precision 4-input ADC (November 2023)
  • 1-24V USB Power Supply (October 2024)
  • 1-24V USB Power Supply (October 2024)
  • 14-segment, 4-digit LED Display Modules (November 2024)
  • 0.91-inch OLED Screen (November 2024)
  • 0.91-inch OLED Screen (November 2024)
  • 14-segment, 4-digit LED Display Modules (November 2024)
  • The Quason VL6180X laser rangefinder module (January 2025)
  • TCS230 Colour Sensor (January 2025)
  • The Quason VL6180X laser rangefinder module (January 2025)
  • TCS230 Colour Sensor (January 2025)
  • Using Electronic Modules: 1-24V Adjustable USB Power Supply (February 2025)
  • Using Electronic Modules: 1-24V Adjustable USB Power Supply (February 2025)
Items relevant to "Part 3: Finishing our new 6GHz+ Digital Frequency Meter":
  • 6GHz+ Touchscreen Frequency Counter PCB [04110171] (AUD $10.00)
  • Short Form Kit for the Micromite Plus Explore 100 (Component, AUD $75.00)
  • Case pieces for the 6GHz+ Frequency Counter (PCB, AUD $15.00)
  • Software for the 6GHz+ Touchscreen Frequency Counter (v1.01) (Free)
Articles in this series:
  • 0.01Hz - 6+GHz touchscreen frequency meter, Part 1 (October 2017)
  • 0.01Hz - 6+GHz touchscreen frequency meter, Part 1 (October 2017)
  • Touch-screen 6GHz+ Frequency Counter, part II (November 2017)
  • Touch-screen 6GHz+ Frequency Counter, part II (November 2017)
  • Part 3: Finishing our new 6GHz+ Digital Frequency Meter (December 2017)
  • Part 3: Finishing our new 6GHz+ Digital Frequency Meter (December 2017)

Purchase a printed copy of this issue for $10.00.

by Andrew Pullin Interfacing with the Raspberry Pi for Beginners While the Raspberry Pi (RPi) micro computer is very popular and has a very large user base, not too many people are aware that the RPi’s GPIO interface can be used for some very interesting applications, such as driving fancy graphics displays. As well, it can provide a user-configurable clock and can interface easily with third party sensors and other equipment. Finding out how to do this stuff is tricky though and this article will reveal how to go about it. T he Raspberry Pi was originally developed by the Raspberry Pi Foundation, a UK Charity, to promote the teaching of basic Computer Science in Developing Countries (see siliconchip.com.au/link/aagc). But the popularity of the cheap, single-board computer (SBC) has seen it explode onto the world market and its uses are much wider than originally intended, including as a powerful embedded controller. The GPIO pins The General Purpose Input Output (GPIO) connector of the RPi is typically a 40-pin header on the board but there is a little bit more to it than that. Some of the pins connect directly to the central Broadcom BCM28xx System on a Chip (SOC) IC. Some provide power supply rails and some ground connections. These pins are unfortunately not connected in an easy-toremember way. This is partly due to the fact that the slightly different Broadcom SOCs used in the various Raspberry Pis have different interface circuitry. This causes some compatibility issues between earlier and later versions of the RPi. The Raspberry Pi 1 Models A+ and B+, Pi 2 Model B, Pi 3 Model B and Pi Zero/Zero W have a 40-pin GPIO header labelled J8. The original Raspberry Pi 1 Models A and B have a 26-pin connector instead, with 14 connections missing. To make things even more confusing, the Model B rev. 2 also has an extra 8-pin header labelled P5 on the board (and for some reason, P6 on the schematics) offering access to an additional four GPIOs. Another point of difference is that Models A and B provide access to the ACT status LED via GPIO pin 16 while Models A+ and B+ provide the same access via GPIO 47 as well as the power status LED via GPIO 35. GPIO pin assignments For the moment though, let’s look at the 26/40-pin main GPIO header. Fig.1 shows the pins on this header, numbered according to their position and colour-coded based on their function. The accompanying legend indicates the meanings of these colours. Unfortunately, this is not how the pins are Enlarged for clarity, this shows not only the GPIO header (the double row of pins along the top, labelled J8 on the PCB) but also the identification of this particular RPi (a Raspberry Pi 3 Model B V1.2). 34 Silicon Chip Celebrating 30 Years siliconchip.com.au HDMI micro USB Power DSI Display Port CSI Camera Port Micro SD (opposite side) USB 2 Ports Bluetooth 4.1 WiFi GPIO Pins Network CPU, GPU, Memory USB 2 Ports An enlarged version of the Raspberry Pi 3, identifying the various interfaces. The one we are particularly interested in is the row of 40 header pins (2 x 20) along the edge of the board, labelled GPIO (General Purpose Input Output) Pins. This article gives a broad range of uses for the GPIO which you otherwise might not have been aware of. actually mapped to the RPi processor. This is shown in Fig.2 – ignore the shading for now and just compare the pin numbering to Fig.1. As you can see, it is quite different. The numbering scheme in Fig.2 shows the RPi I/O pin number that’s connected to each pin on the header (referred to below as the BCM pin number, which is short for Broadcom). This may seem a little confusing but in many cases, you can simply connect whatever digital input/output that you need to control to any one of these pins and then change your software to communicate using the number shown in Fig.2. The good news is that this numbering scheme applies to any RPi with a 40-pin GPIO header. There are some cases where you need to use a specific pin for a specific purpose, though. This is indicated by the extra labels in Fig.2 and the shading around the outside of the pins, which shows how the pins are related in terms of function. The special functions include one UART, two SPI buses, two I2C buses, three PWM outputs and three square wave outputs. Fig.1: the pins on the GPIO header, numbered according to their position and colour-coded based on their function. siliconchip.com.au In case you aren’t familiar with the acronyms: UART stands for Universal Asynchronous Receiver/Transmitter and is basically a 3.3V bidirectional RS-232 serial port. SPI is Serial Peripheral Interface, a higher speed (and simpler) bidirectional serial bus. I2C stands for Inter-Integrated Circuit and is a slower serial bus which only requires two wires (plus ground) and it can be shared by multiple devices, unlike SPI or VART. PWM stands for Pulse Width Modulation and allows you to produce a square wave with a variable frequency and duty cycle, eg, to control the brightness of LEDs, motor speed and so on. The square wave/clock outputs are similar except that only their frequency can be varied; the nominal duty cycle for these outputs is 50%. These functions are shared with the general purpose I/O pins, meaning the pins labelled with special functions can be switched between normal inputs, normal outputs or those dedicated functions. So if you want to use one of Fig.2: the RPi I/O pin number that’s connected to each pin on the header (referred to in the text as the BCM pin number, which is short for Broadcom). Celebrating 30 Years December 2017  35 NOTE: URL “SHORTLINKS” URLs (website addresses etc) in this feature have been shortened to SILICON CHIP Shortlinks, saving you a lot of keystrokes (and mistakes!). In the online version, clicking on these shortlinks will take you direct to the relevant website. these functions, you will have to use a pin or set of pins as indicated in this diagram. Before we move on, here’s a hint: before hooking any hardware up to the GPIO port, first figure out which of these dedicated-purpose pins you need to use. You can then use the remaining pins for other digital I/O tasks without any conflicts. Serial bus connections UART connections are simple; TXD is the transmit pin and RXD is the receive pin. You could arrange for two Raspberry Pis to communicate with each other by connecting TXD on one to RXD on the other and vice versa, then making a ground connection between the two. The I2C buses also have two pins but they have different purposes. SDA (SD) is the data pin and SCL (SC) is the clock pin. All devices on an I2C bus have their SDA pins joined together and their SCL pins joined together. On each bus, there should be a single pull-up resistor between each of these two networks and the 3.3V supply rail. The values of these resistors depends on the bus speed. For more information, see siliconchip.com.au/link/aagd SPI buses have at least three pins. SCLK is the clock and this is wired directly between the master and each slave device on the bus. MOSI stands for “master out, slave in” and MISO “master in, slave out”. Like SCLK, all identical pins on the bus are joined together. SPI bus zero has two additional chip enable/slave select (CE) pins which can be wired to two separate slaves and these are pulled low to indicate which slave the master is communicating with at any given time. You can have more than two slaves on the SPI0 bus but then you will need to use additional GPIO pins, set as outputs (normally high) and pull them low manually before initiating communication with that slave (and bring it high when finished). The SPI1 bus has three hardware CE pins, so you can have one more slave than on SPI0 before you need to resort to manually driving the chip enable/slave select pins. We have more details on using these serial buses below but first let’s look at the other functions available on the GPIO header. Power supply rails You can use some of the pins on the GPIO header to power external circuitry. Pin 2 and 4 are both connected to the 5V rail, which is normally directly connected to the RPi’s power supply (typically a USB charger). Pins 1 and 17 provide a regulated 3.3V supply while pins 6, 9, 14, 20, 25, 30, 34 and 39 are ground connections. These eight ground pins are all joined together by the ground plane on the RPi so it doesn’t really matter which one(s) you use for connecting either power supplies or as a ground reference (eg, as part of a voltage divider). However, you probably shouldn’t use the same ground pin for both purposes. So use at least one dedicated ground 36 Silicon Chip reference pin, while the others can be used as a supply rail. In practice, when powering circuitry from one of the 3.3V or 5V pins, use the nearest ground pin to complete the circuit. Remaining GPIO pins All pins, other than the power and ground pins, can be used as either inputs or outputs. This is configured by the software running on the Raspberry Pi. Pins that are not labelled with special functions in Fig.2 can only be used in this manner while the other (special function) pins can be used as inputs or outputs only if they are not being used for their specific function. You need to be wary when using the GPIO pins as inputs since most of them have pull-ups or pull-downs built into the Raspberry Pi. Referring to the pin numbers given in Fig.2, those labelled 0-8 are pulled high by default while the rest are pulled low. Note that the pins which are pulled high include all four I2C pins plus both SPI-0 Chip Enable pins, which makes sense when you consider their functions. GPIOs which are configured as outputs can drive the digital inputs of other ICs or light LEDs if the current is limited to what the Broadcom chip can handle (16mA each, 51mA total). But they are not designed to drive anything directly that requires high current like motors. Output current can be boosted in various ways, such as by adding transistors or using a third-party HAT (Hardware Attached on Top) board which boosts the current capabilities. Note also that any GPIOs which are driven externally must not be driven below 0V or above 3.3V. This can damage the RPi. For signals which may exceed these limits, you need to use either a series resistor and clamping diodes or a level-shifter IC. None of the RPi pins are 5V-tolerant. If you need to communicate with a digital chip that uses 5V signalling, in many cases, a 3.3V output from the RPi will successfully drive the input of the 5V device. But you’d better check the device’s data sheet to make sure that it will reliably read voltages above 3V as a high level. For signals going from the 5V device to the RPi, you’re best off using a level shifter IC such as the 40109B although there are other approaches. For pins which are programmed as digital inputs, the software can read their value and this will return a value of zero (when the voltage on that pin is low) or one (where it’s at or near 3.3V). Similarly, for pins set as digital outputs, the software can set their value to zero, in which case the voltage will be pulled down to around 0V, or one, in which case the pin’s voltage will be pulled high, close to 3.3V. So that covers the basics of RPi GPIO and you can find tutorials on the internet which show you how to program the I/O pins as digital inputs or outputs. But the devil is in the detail and those details are what makes the RPi really useful. Alternative pin functions The Raspberry Pi Organisation website (siliconchip.com. au/link/aagc) has some very useful information on it about everything Pi but it is sometimes hard to find the more technical information unless you know where and what to look for. Often, it is simpler to just Google for information elsewhere to find it, then search the Pi site separately. Having done the above, I discovered a very useful website at Celebrating 30 Years siliconchip.com.au siliconchip.com.au/link/aage This website provides all the pinouts of the GPIO on the Raspberry Pi and here is where the first surprise comes from. A hidden graphics display function The GPIO header can do more than one thing if you know how and where to look for the information. The first thing I learned was that the GPIO can be used as an up to 24-bit colour display driver called the Parallel Display Interface (DPI). The details for the DPI can be found at: siliconchip. com.au/link/aagf In a nutshell, this interface can be used to drive an RGB display using one of three formats; • RGB24 (8-bit red, 8-bit green and 8-bit blue), • RGB666 (6-bit red, 6-bit green and 6-bit blue) or • RGB565 (5-bit red, 6-bit green and 5-bit blue). FLAT BATTERY... MILES FROM ANYWHERE? We have the PERFECT solution: JUMP START BATTERY The DPI is controlled by the Graphics Processing Unit (GPU) part of the Broadcom SOC and is user configurable via a simple text file in the Linux Operating System that’s typically used on the RPi. IT’S THIS EASY! User-configurable clocks There are three user-configurable General Purpose Clock (GCLK) pins on the GPIO header. These signals are derived from the peripheral clock sources via clock generators with MASH (multi-stage noise shaping) dividers. These allow the GPIO clocks to produce audio signals. Wow! That was unexpected. I can see the need and use for some kind of clock interface on the GPIO interface but to have the capability to drive audio devices out of the box is pretty powerful. As shown in Fig.2, the Clock Pins on the GPIO header are: • Pin 7 (BCM 4): GCLK0 • Pin 29 (BCM 5): GCLK1 • Pin 31 (BCM 6): GCLK2 CAN BE STORED IN YOUR GLOVE BOX POWERFUL HIGH ENERGY BATTERY NO WAITING! SIMPLE AND EASY TO USE CLIP ON AND START SPECIA XMAS OFFLE R: O rder BEFO RE Christma s receive aand FR E LED 12V cE amp light! ing IT CAN ALSO BE USED TO CHARGE YOUR MOBILE POWER CAMP LIGHTS Dallas 1-wire protocol (w1) This one is pretty technical and very confusing for beginners, especially since it lies and actually needs two wires (one for data and one for ground). Basically, this is used in a very simple master/slave configuration to communicate with certain devices such as the DS18B20 digital temperature sensor. The default pin used for this protocol is pin 7 on the GPIO header (BCM 4) but this can be changed. If you are interested in doing this, refer to our article titled “1-Wire Digital Temperature Sensor for the Raspberry Pi” in the March 2016 issue (siliconchip.com.au/Article/9849) which has all the details. Pulse Code Modulation (PCM) PCM is a digital representation of a sampled analog signal. The Raspberry Pi can produce this form of digital audio output which can be fed to a Digital to Analog Converter (DAC) for high quality sound. The output signal from a DAC chip is normally a couple of volts but with only a weak drive strength so it will probably need to be fed to an audio amplifier before it can power headphones or speakers. You can also use this PCM interface to connect to a highspeed ADC (analog-to-digital converter), ie, the opposite of a DAC. Or you can even use it with a CODEC, which is basically a synchronised DAC and ADC in one package. siliconchip.com.au Great XMAS Present It features 2 USB 5V outputs – One at 1 AMP, One at 2 AMPS For Mobile and Tablet The 12V DC Output socket can be used for camp lights, etc This powerful battery weighs only 450g It will fit comfortably in your glove box – yet will easily start your car when your battery goes flat. Don’t get caught waiting for a new battery at inflated prices It can also be used to charge your mobile phone or tablet – it is a powerful 16Ah BATTERIES AND CHARGERS ARE OUR BUSINESS Suppliers of Quality Batteries for over 30 Years Celebrating 30 Years Unit 9, 15 Childs Rd, Chipping Norton NSW 2170 email: info<at>premierbatteries.com.au Website: www.premierbatteries.com.au TEL: 02 9755 1845 FAX: 02 9755 1354 December 2017  37 The PCM function is available on the following four pins: • • • • Pin 12 (BCM 18): PCM CLK (Clock) Pin 35 (BCM 19): PCM FS (Frame Synchronisation) Pin 38 (BCM 20): PCM DIN (Data In) Pin 40 (BCM 21): PCM DOUT (Data Out) There are a number of tutorials on how to use this PCM interface on the Internet. This one from AdaFruit is useful: siliconchip.com.au/link/aagg Note that CLK is the bit clock and there will be one pulse on this line for every bit transmitted to the DAC (DOUT) or received from the ADC (DIN). The FS pin will typically produce one pulse for every set of samples transmitted and/or received. In the case of a stereo DAC/ADC/CODEC, this is one pulse for every pair of samples (ie, left and right channels) and the current polarity of the FS signal indicates which channel is being transmitted/received. Depending on the sampling rate and resolution, these signals can have quite high frequencies; up to around 24MHz. So signal routing can become an issue. Inter-Integrated Circuit (I2C) details I2C is a serial communication protocol originally developed by Philips Semiconductor to enable simple low level communication between chips and uses two wires plus ground, as described earlier. It is now a communication standard in the computing world for sensors, microcontrollers, port expanders and more. Sensors! Microcontrollers! Now we are talking. I2C is supported by a large range of devices, especially devices which don’t need to get a lot of data in or out; this is one reason why most sensors support I2C. You can also use I2C to communicate with another micro, however, this will be slower than if you use SPI (as described below). The primary I2C port on the RPi is I2C1 and uses the following two pins: • Pin 3 (BCM 2): I2C1 SDA (data) • Pin 5 (BCM 3): I2C1 SCL (clock) As the I2C Pins on the GPIO port have built-in pull-up resistors, you don’t need to add external resistors for normal low-speed signalling. However, you may need to add extra pull-up resistors for higher speeds. Again, there are some very good tutorials on the Internet and if you are serious about using your RPi then learning as much as you can about I2C cannot be a bad thing. Try this one: siliconchip.com.au/link/aagh There is a second I2C port (known as I2C0) on the following pins: • Pin 27 (BCM 0): I2C0 SDA (EEPROM Data) • Pin 28 (BCM 1): I2C0 SCL (EEPROM Clock) As a beginner, I would strongly advise that you do not use I2C0. The reason for this is that it is wired up directly to the EEPROM on the RPi. An EEPROM is an Electronically Erasable Programmable Read Only Memory. The one on the RPi can be wiped and reprogrammed using these GPIO pins and that could make your RPi less useful than a brick if you don’t know what you are doing. Don’t say I didn’t warn you! Serial Peripheral Interface (SPI) details The SPI is also known as the four-wire serial bus and 38 Silicon Chip Shown here for comparison and identification, these are the front (above) and rear (opposite) views of the Raspberry Pi Model 3 micro PCBs. allows you to do some really cool things. One of the most common uses of SPI is to communicate with other devices like Arduinos, enabling you to load Sketches directly into them. As described earlier, there are two SPI buses available on the 40-pin GPIO. The first one, SPI0, uses the following pins: • • • • • Pin 19 (BCM 10): SPI0 MOSI (Master Out, Slave In) Pin 21 (BCM 9): SPI0 MISO (Master In, Slave Out) Pin 23 (BCM 11): SPI0 SCLK (Serial Clock) Pin 24 (BCM 8): SPI0 CE0 (Chip Enable/Slave Select 1) Pin 26 (BCM 7): SPI0 CE1 (Chip Enable/Slave Select 2) The second port, SPI1, is on: • • • • • • Pin 38 (BCM 20): SPI1 MOSI Pin 35 (BCM 19): SPI1 MISO Pin 40 (BCM 21): SPI1 SCLK Pin 12 (BCM 18): SPI1 CE0 Pin 11 (BCM 17): SPI1 CE1 Pin 36 (BCM 16): SPI1 CE2 One of the things that make the SPI peripherals so versatile is that they have several “master modes” which allow communications with different kinds of chips. The first mode is “Standard Mode” which is the normal 3-wire protocol (not including chip select or ground). The second is “Bi-Directional Mode” which uses one less wire. MISO is not used and MOSI instead functions as MOMI (Master Out, Master In) where it functions as either MISO or MOSI depending on whether data is being transmitted or received. The third mode is “LoSSI Mode” which stands for Low Speed Serial Interface. This is a 9-bit communications mode typically used to interface with small LCD screens. A great explanation of all these modes is available on the Raspberry Pi Foundation website here: siliconchip. com.au/link/aagi UART serial port details As we said earlier that a UART is typically used for RS232 communications. The U for Universal means that transmission speed and data format are configurable. As it is an asynchronous serial port, there is no need for a Celebrating 30 Years siliconchip.com.au nisation clock). For the two possible pin assignments for each of these functions, see the link above. Further information The Broadcom BCM2835 SOC was used in the original RPi Model A1/1+ and Model B1/1+; the BCM2836 on the Model B2; and the BCM2837 on the Model B2v2.1 and Model B3. All of these SOCs are backwards-compatible with the BCM2835 and a large amount of very technical and very useful information can be found in the BCM2835 ARM Peripherals Datasheet at: siliconchip.com.au/link/aagl No analog inputs or outputs With the exception of the micro SD socket (right side of the PCB) there is virtually no connection made to the rear of the PCB. separate clock signal and so two wires can be used for full duplex communications (simultaneously transmitting and receiving data). This type of serial port has been used for decades to get different devices to talk to each other. Back in the 1980s, I used to work at a Cabling Company in Adelaide and I had a book with about 100 different serial port configurations and how to wire them up. It impressed me back then and not much has changed. The pins used for UART are: • Pin 8 (BCM 14): TX/TXD (transmit) • Pin 10 (BCM 15): RX/RXD (receive) Since we also need a common ground, there is a convenient one at pin 6. A good general discussion of serial communications on the RPi can be found here: siliconchip. com.au/link/aagj JTAG Most 32-bit and 64-bit microprocessors support an interface known as JTAG which stands for “Joint Test Action Group”. This can be used for programming and testing various chips and the chips can be chained together so that a single JTAG interface can be used to communicate with all of them, simplifying circuit board layout. As well as programming chips, JTAG can be used for “boundary scan”, which allows a device to inspect and possibly change the state of the pins on an IC. For debugging, the JTAG interface can provide one or more “test access ports”. Note that using a JTAG interface generally requires complex and quite specialised software and while we have successfully used it to program some devices, it really is a lot of work to get up and running (and beyond the scope of this article). If you want to know more then Google is your friend. This is a very good tutorial for debugging a Raspberry Pi using JTAG but we have to warn you that it’s heavy going: siliconchip.com.au/link/aagk The RPi has two possible sets of JTAG pins, with only the TRST (test reset) function fixed to BCM pin 22. The other JTAG functions are TDI (data in), TDO (data out), TCK (clock), TMS (test mode select) and RTCK (synchrosiliconchip.com.au There is only one sticky point about the RPi GPIO compared to other micros and this is that the “out of the box” version has no analog inputs or outputs. This is an issue because there are a multitude of sensors available on the market and not all of them have a digital output so you can’t directly connect them to the Pi. The operating word here is “directly”. The Pi can create analog signals (sort of) by using PWM and then passing this signal through a low-pass filter but that’s pretty crude and only works well in certain situations. You can use the PCM interface described above with a DAC but that requires quite a few extra components. The most common solution for analog I/O is to plug in a HAT board designed for this purpose. (HAT stands for Hardware Attached on Top).That is certainly an easy way to do it, but of course HATs cost money and so the Community has been hard at work problem solving and come up with a few ideas of its own. While it is of limited use, you could consider combining an external comparator with the PWM or PFM functions to form a “Poor Man’s ADC”, as described here: siliconchip. com.au/link/aagm Another common method is to use an off-the-shelf ADC module such as one with the MCP3004/3008 and interface to it using the GPIO pins. One of the great things about the MCP3004/3008 is that they have built-in SPI interfaces. A tutorial showing how to do this can be found at: siliconchip.com.au/link/aago How to access GPIOs through software There are a few different ways to control the GPIO pins from software on the RPi. Some are supplied with the RPi operating system and some are from third parties. The Raspberry Pi Foundation recommends running the NOOBS operating system, which is a custom-built version of Linux. But it is not the only operating system available. There are several versions of Linux, Windows 10 IoT Core and one called RISC OS. If you’re using the recommended NOOBS, you will already have most of the software libraries that you need. The Raspberry Pi Foundation recommends using the Python programming language that comes standard with NOOBS and the C language is also very common; it too comes standard. Each of the many tutorials I discovered had various libraries and technologies to install depending upon the application, but one such common library is WiringPi, which Celebrating 30 Years December 2017  39 can be found at siliconchip.com.au/link/aagn According to their web page, “WiringPi is a pin- based GPIO access library written in C for the BCM2835 used in the Raspberry Pi. It’s released under the GNU LGPLv3 license and is usable from C, C++ and RTB (BASIC) as well as many other languages with suitable wrappers. It’s designed to be familiar to people who have used the Arduino ‘wiring’ system.” Beginners may find Python programming easier. We published an article in the November 2016 issue titled “Using your Raspberry Pi with a smart-phone as WiFi-controlled switch” (siliconchip.com.au/Article/10416). It gave detailed set-up procedures and sample code which shows how to control some GPIO outputs from a Python web script. That code could be adapted to perform other tasks quite easily. If you are familiar with C/C++ then we suggest that you install WiringPi and give it a go. After that, the sky is the limit. Conclusion While Raspberry Pi was intended as a low-cost computer for educational purposes, the GPIO port also gives users the ability interface the Pi to the real world quickly and easily. I now have two Raspberry Pis and a couple of Arduinos and what started out as a simple search to learn a bit more about how to make them talk to the world has ended up as this article. I hope readers can get use it as a jumping-off point for their own projects based on the Raspberry Pi. References • A general overview of the Raspberry Pi from Wikipedia: siliconchip.com.au/link/aagq • Official GPIO documentation: siliconchip.com.au/link/ aagr • Comprehensive GPIO Pinout guide for the Raspberry Pi: siliconchip.com.au/link/aags • Compute Module I/O pins: siliconchip.com.au/link/ aagt • Display Parallel Interface details: siliconchip.com.au/link/aagu • BCM2835 ARM Peripherals Datasheet from Broadcom, 2012 (PDF): siliconchip.com.au/link/aagv • Raspberry Pi debugging with JTAG (PDF): siliconchip.com.au/link/aagw • Pulse Code Modulation interface: siliconchip.com.au/link/aagx • I2C with Raspberry Pi: siliconchip.com.au/link/aagy • SPI with Raspberry Pi: siliconchip.com.au/link/aagz • Using UART on Raspberry Pi with Python: siliconchip.com.au/link/aah0 • GPIO Interface library for the Raspberry Pi: siliconchip.com.au/link/aah1 • MCP3004/3008 4/8-channel 10-bit ADCs data sheet (PDF): siliconchip.com.au/link/aah2 The RPi website, raspberrypi.org, has a wealth of information and references to help you on your way. 40 Silicon Chip The Raspberry Pi 3 is distributed in Australia by element14. See siliconchip.com.au/link/aagp It is available through a number of retailers including Altronics and Jaycar. SC Celebrating 30 Years siliconchip.com.au