Control the Raspberry Pi's GPIO from a Qt4-Based Graphical Application

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
Qt GPIO control GUI over SSH with X11 Forwarding

Install the Qt4 libraries/Tools 

The first step would be to install the Qt4 libraries & tools. This can be done by running the following command on the Raspberry Pi:

sudo apt-get install qt4-dev-tools libqt4-dev libqt4-dev-bin qt4-qmake

That's it!.

Write the Qt4 GUI application

The next step is to write a simple QWidget based application that controls the  GPIO! While some of the source code will be shown below, please download it from here ...

more ...

Drive a Servo Motor with The Raspberry Pi's PWM1 Peripheral in C++

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 Mini Servo
SG92R Micro Servo

The shaft on servo motors typically only rotates 180 degrees (sometimes only 90 degrees). The exact angle of rotation  of the shaft on a servo motor is usually controlled by particular timing ...

more ...

Add Analog to Digital Conversion Capability to The Raspberry Pi Without an ADC Chip

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 analog 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 summarized 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 ...

more ...

Controlling the Raspberry Pi's GPIOs using Direct Register Access in C++

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 ...

more ...

Accessing The Hardware PWM Peripheral on the Raspberry Pi in C++

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'.

The rpiPWM1 Class

I developed C++ class 'rpiPWM1' that  maps the PWM1 peripheral to GPIO18 and is able to control the PWM frequency, resolution, duty cycle ...

more ...

LightSpeak - Speaking Light Meter

LightSpeak, the hardware

In this entry I will outline an Arduino based project that I undertook as part of the Ottawa Arduino/Hacker Challenge for August 2014. The objective of the Challenge is to build  a  project around the Light dependent resistor (LDR) also known as a cadmium Sulphide (cdS) photoresistor. The LDR is a light sensor whose resistance varies with light intensity. The LDR's resistance reaches its maximum under total darkness and is typically very small or zero in very bright/sunny conditions. It's a really fun resistor/sensor to play with!

Figure 1. A Light Dependent Resistor (
A Light Dependent Resistor (

I decided to build a 'speaking' light intensity meter using  two LDR sensors, an Arduino and a bar graph LED. For the 'speaking' part of the project, the Arduino will send over serial a value representing light intensity to a custom 'lightSpeak' application running on ...

more ...

Cross Compiling Qt4 Applications for the Raspberry Pi

This blog post will demonstrate how to cross compile Qt4 applications for the Raspberry Pi (RPi).  The approach taken in this entry is a little different from that taken by other tutorials such as this or this. The complete cross compilation of the Qt4 or Qt5 libraries will NOT be covered. Instead,  the Qt4 libraries available in the Raspbian's repos will be installed on the Raspberry Pi and the RPi's root filesystem will then be mounted onto the PC over the network using SSHFS. The RPi's  cross compiling toolchain will then build qt apps by compiling Qt4 source code on the PC and linking it to the Qt4 libraries available via the SSHFS-mounted RPi filesystem.

Simple QT4 application cross-compiled on PC Simple QT4 application cross-compiled on PC

The challenge with such an approach is the inability to use either the native qmake on the PC or the RPi. Both were built to be ...

more ...