5V GPIO, PWM and ADC on Raspberry Pi

This is a long post, I’m sure most will stop after the “too long, didn’t read” section but if you’re looking to interface 5v sensors, multiple PWM outputs and multiple analog inputs to the Raspberry Pi with a simple interface and code ready to go then read on.

TL;DR;

  • Pi does not have 5V GPIO, >1 PWM, Analog ports/pins but Mr. Bit needs these (Arduino does)
  • Arduino can’t be used to be master onboard controller for RC and/or Automation in PiWars
  • Team Mr. Bit rising to challenge and decision is Pi will have to be master onboard controller for RC AND automation
  • Requirements: Supply and tolerate 5V and >= 2xPWM outputs and >=3xADC inputs
  • Options discounted:
    • new sensors 3V3 digital & motor driver – lowers quality of components (+extra cost)
    • Separate level shifters and converts – complex (we’re not that skilled)
    • Gertboard – does’t have the features we need and a whole bag of others
    • Raspi Duino – runs at 3V3 (wrong voltage) other than that is no different from any other Arduino option
    • Seeeduino – original approach but preferred option simplifies integration
  • Preferred approach: GrovePi+* – meets all requirements and simplifies integration/more elegant design than seeeduino.

* caveat – that’s not to say it’s “better” than the others, just more aligned to our requirements and a much simplified interface!

Overview

Over the past few posts we’ve expressed concern that the sparkfun monster moto and the sharp IR distance sensors we have chosen for Mr Bit (PiWars 2017 competition entry) requires at least 2 pwm pins, 3 analogue pins (adc) and 5V gpio pins . Unfortunately the Pi itself only has 1 PWM pin, no adc and operates at 3V3 across all its gpio pins.

The approach we were taking was to use an Arduino (in fact the motor driver is an arduino shield) to control the motors with PWM 0 – 5v which is needed for the driver as well as making use of the arduino’s adc analog pins at 5v for the sharp sensors. The Pi would then interface to the Arduino, probably via I2C or USB to send commands to the Arduino for remote control (RC) and setting it into “autopilot” for automation.

This would probably be fine if all of PiWars challenges were remote control with the Pi receiving instructions from a handheld bluetooth gamepad or phone and passing onto the Arduino to control the motors. However, when it comes to the automation an optimised system would delegate autonomous control to the Arduino at this point, as it’s this unit that’s sensing and controlling – but this is PiWars and the core unit for all control needs to be the Raspberry Pi.

twitter thread - piwars pointing rPi must be main controller

Part of twitter conversation discussion Arduino involvement

Although the conversation suggests that the Pi needs to manage the RC and/or automation we’ve decided we’re no longer happy that Mr Bit will have only partial control via the Pi so now we want the Pi to actively manage both RC and automation (also in the hope we get extra kudos points for doing this too!). One option would be to change our components to be 3V3 and digital but given we chose them for their quality and spent quite a bit of money on them we’re going to discount this option, unless an alternative turns out to be too complex or/and expensive.  So before we take a look at other options let’s take a look at Mr Bit’s requirements:

Requirements

  • output 2 channels of 0 – 5V PWM to motor driver
    • it could be single and we could run the connection in parallel but that would mean a reduction in manoeuvrability
    • it could be managed via software PWM but this limits the accuracy and smoothness of the PWM (also feels brutal to a non-hardware-pwm gpio).
  • input at least 3 analog to digital conversions
    • might need more but 3 min for the Sharp Sensors (1 front, 1 each side)
  • supply and tolerate 5V
    • the motor driver needs to be 5V
    • the sharp sensors (and possibly others) need to be 5V
  • Protect self (GPIO) from 5V supply
    • 5V into a port on the Pi will damage the Pi.
  • Manage all instructions to motors from Pi
    • no “autopilot” mode delegated to an Arduino.

What are the options we’ve reviewed?

1. Separate level shifters, PWM and ADC converters

components

Level Shifter, PWM Expander and ADC chip

Here we could use a few level shifters to convert the 3V3 to 5V and back between the GPIO and various components in order to manage the different voltages from the Pi and the sensors/motor drivers. We would also integrate a couple analog to digital converter chips for a number of inputs so that analog readings from sensors could be taken. Finally we would use something like the PC4342 PWM expander and interface this over I2C to offer us more PWM chips.

Pros:

  • All requirements are met
  • Even more PWM capabilities than expected

Cons:

  • Complicated to configure and interface, lots of I2C and voltage shenanigans
  • Limited electronics skills in our team
  • Risk of routing 5V to 3V3 GPIO as we don’t really know what we’re doing
  • Limited time to set up (we’ve less than 2 months left to complete)

Cost: ~£30

2. Use Gertboard and WiringPi to interface Pi

gertbaord

The Gertboard – technically brilliant but complex

The Gertboard looks like a comprehensive conversion to features that are limited or not present on the pi. For example there are hardware analog to digital and digital to analog conversion chips and an Atmel ATmega 328 chip to make use of. The interface to the Gertboard uses native GPIO pins converting them to other functions on the getboard as required. However, the Gertboard operates on3V3 so we would still need the level shifters, and there is limited number of ADC/DAC ports on the board. The gertboard is best documented board we have come across (from a geeky pov) and the pdf guide for it has helped us understand some of the more esoteric nuances of our options.

Pros:

  • Uses native GPIO pins (in most cases)
  • Implemented via WiringPi or RPi.GPIO libraries

Cons:

  • Board operates at 3V3
  • only 2 ADC inputs
  • ATmega chip is standalone, Pi only used to program it.
  • More features than we need including buttons, motor drivers and drive controllers… (an eclectic mix).

Cost: ~£35

3. Raspi Duino

duino

Arduino based hat – programmable from the RPi

The Raspi Duino is an Arduino based hat that first impressions suggest it might be quite useful for our needs. However, the hat simply an interface in order to program the ATmel328 chip, using the Pi’s SPI GPIO ports to interface with the chips programmable interface. This is basically an arduino with stacked headers to interface the Pi to program it. It also runs at 3V3 so isn’t capable of controlling our motor drivers efficiently nor operating our IR sensors.

Pros:

  • Simple interface to Pi

Cons:

  • Not 5V (operates 3V3)
  • Standalone Arduino
  • No abstraction to comms, (uses Minicom and SPI)

Cost: ~£15

4. Standalone Arduino (original Seeeduino approach)

Seeeduino

Nice colours on this neat Arduino

We originally had an Seeeduino controlling the motor drivers which led to this review of our design decision. The Arduino operates at 5V and has all the ADC and PWM pins we need. However, our approach was to delegate control of the motors to the Arduino for automation as there’s a decent amount of redundant data transfer and processing passing back sensor readings to the Pi and then control commands back to the Arduino. However, this _is_ PiWars and the Pi has to control the motors (even if it is via an ATmega chip). Therefore this approach would like the other Ardunio options result in comms over I2C (or USB) with the Pi and the Pi has the commanding control logic based on any sensor readings from the Arduino as well as any RC input from a user-based controller.

Pros:

  • Meets all Mr Bit requirements
  • Already have all components

Cons:

  • Interface not as straight forward as other “hat” options
  • Will need method of controlling Arduino via Pi code
  • Cost: £0

5. GrovePi+ Hat (Preferred Option)

Another ATmega Hat with JST connectors

The GrovePi+ is an ATmega328 based hat which exposes some of the PWM and ADC pins on the chip via JST connectors which have become the chosen interface connector of the Seeed Grove system. I like Grove, it’s simple, comprehensive and the number of sensors and their quality choice of component is impressive. The GrovePi operates at 5V and whilst it is an Arduino-based hat the DexterIndustries approach to operating it is slightly different to a standalone. They have created a sketch (arduino program) for the ATmega328 which acts as interface and abstraction of commands commonly used directly within the Arduino wiring language. This means that you actually write similar programming in Python on the Pi similar to Arduino methods and results in commands being passed to the Arduino from the Pi over I2C to then be converted into native Arduino command on the ATmega328 – the result is realtime control using commands from the Pi direct to the Ateml chip taking advantage of the ATmega328 capabilities with 2 way comms between it and the Pi.

Pros:

  • Meets all requirements of Mr Bit
  • Elegant interface from Pi to ATmega
  • Not a stand alone Arduino approach
  • Simplifies code (ready Python abstraction over I2C)
  • provides just/grove support and connector interfaces
  • Interface Pi to ATmega code already available

Cons:

  • Will cost a little to get one
  • Could implement same code/approach with

Cost: ~£25

Summary:

Given our limited electronics skills we can’t take the risk of using separate dedicated components though this option does feel appealing for the near future. The only other viable options is to use an ATmega328 and either a standalone Arduino or the GrovePi+. For us the benefits that the GrovePi will give us and our time constraints means this is our favourite choice.  Although we could load the GrovPi code to our already owned Arduino, interfacing the pi via some jumpers for the I2C wire, we think the effort that has gone into GrovePi design, code and the neat JST connectors is worth the small investment.

Leave a Reply

Your email address will not be published. Required fields are marked *