Many of the previous blog entries on this site demonstrated how to use the IO interfaces on the Raspberry Pi using command line applications. In this entry, the use of Qt4-based graphical user interface (GUI) application to control the Raspberry Pi’s GPIO will be examined. While a cross compilation approach is possible, only native compilation will be considered in this entry for the sake of simplicity.
Qt GPIO control GUI over SSH with X11 Forwarding
In a previous entry, I demonstrated how to generate PWM waveforms with the RPi’s PWM1 peripheral. The ‘rpiPWM1′ C++ class was developed to facilitate this process. In this entry, I will look at how the ‘rpiPWM1′ class can be expanded upon to facilitate the control of a servo motor.
Servo motors are special kind of motors used in applications that require precise position control. Servo motors consist of a motor, gears, a potentiometer and an electronic circuit. The electronic circuit and potentiometer are used to create a tight feedback loop that facilitates the required position control. Servo motors usually have a 3 wire connector that includes ;
- Ground – Brown
- Power – usually 6V – red
- Signal – orange
SG92R Micro Servo
One of the few disadvantages of the Raspberry Pi is that it lacks a built-in analog to digital converter(ADC). This can be remedied by connecting a dedicated ADC chip to the RPi Board via SPI (or even I2C). But there are other ways to get analogue to digital conversion going on the Raspberry Pi.
This entry demonstrates how to add analog to digital conversion capabilities to the Raspberry Pi with a few external components (a comparator, two resistors and a capacitor) and some software. Why not just use an ADC chip you say! I say why not! The objective here is to be playfully clever and possibly learn something along the way.
The entire process is summarize as follows: The PWM1 peripheral in the Raspberry Pi is used as a digital to analog converter (DAC) with the aid of a simple passive RC filter. This DAC outputs a variety of analog voltages that the comparator chip compares with the target analog voltage (to be converted to a digital value). The comparator’s output indicates to the successive approximation algorithm, which bits of the final digital result should by ’1′ and ’0′. The successive approximation algorithm will be running on the Raspberry Pi.
This ‘hack’ is not new and has been used many times with microcontrollers that lacked built-in ADC’s, but had PWM generators. In some cases where the microcontrollers also lacked PWM generators, R2R ladders were used as DACs instead.
I’ve yet to see this sort of ‘hack’ applied to the Raspberry Pi however.
The schematic for the circuit is shown below.
Basic ADC Hack schematic
In this blog entry I will present the mmapGpio class that provides basic access to all the GPIO’s on the RPI’s 26-pin header using direct register access. The advantage of this approach to GPIO control is that one can bypass Linux and talk directly with the GPIO registers which enables much faster GPIO toggling speeds. Using this approach I was able to get GPIO toggling speeds that exceed 25MHz!!!! That’s much faster than the toggling speeds that can be achieved using the safer but slower SYSFS approach to GPIO control.
The mmapGpio class is intentionally designed to be simple but useful. A great feature of this class is that a single instance can be used to control the all the RPI’s GPIOs. The mmapGpio class with two examples and makefiles can be downloaded from here.
We will now look at the two examples that demonstrate the use of the mmapGpio class.
In this Blog entry I will demonstrate how to access the single hardware pulse width modulation (PWM) channel available on the Raspberry Pi. The BCM2835 SOC on the Raspberry Pi has two PWM peripherals; PWM1 & PWM2. Only the PWM1 peripheral can be mapped onto a GPIO pin (GPIO18) available on the RPi’s 26-pin header. It’s important to note that both the PWM1 & PWM2 peripherals are used by Raspbian to generate audio, so make sure that the RPI is not generating audio while accessing the PWM peripherals. Since Raspbian/ Linux already assigns the PWM peripherals for audio generation, there’s strictly no direct ‘proper’ way to access the PWM1 peripheral from userspace without accessing the hardware registers directly by ‘mmaping into /dev/mem’.