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

Continue reading

Posted in Raspberry Pi, Raspberry Pi Peripherals | Leave a comment

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 analogue to digital conversion going on the Raspberry Pi.

In this entry I will demonstrate how to add analog to digital conversion capabilities to the Raspberry Pi by using 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! I’m just trying to be playfully clever and possibly learn something along the way.

Basically we will use the PWM1 peripheral in the Raspberry Pi as a digital to analog converter (DAC) with the aid of a simple passive RC filter. This DAC is then used to output a variety of analog voltages that the comparator chip compares with the target analogue voltage to be converted to a digital value. The comparator’s output is used to indicate to  the  successive approximation algorithm, which bits of the final digital result should by ’1′ and which should be ’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 would be 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

Basic ADC Hack schematic

Continue reading

Posted in Raspberry Pi, Raspberry Pi Peripherals | 6 Comments

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 use of the mmapGpio class.

Continue reading

Posted in Raspberry Pi, Raspberry Pi Peripherals | 1 Comment

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

Continue reading

Posted in Raspberry Pi, Raspberry Pi Peripherals | 3 Comments

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!

Continue reading

Posted in Uncategorized | Leave a comment