Posts: 18
Joined: Mon May 27, 2013 9:45 am

Add ADC Functionality to RPi with EPi

Thu Aug 01, 2013 9:35 am

Raspberry Pi set aside some expansion interfaces to connect with peripheral devices and extend the functionalities, like SPI, I2C, UART and GPIO. However, Raspberry Pi has difficulty in acquiring environmental signals such as temperature, moisture, and air pressure, due to lack of analog input interface. Embedded Pi addressed this issue – Taking the advantage of Embedded Pi ArduinoTM interface, we can connect analog sensors to Embedded Pi via ArduinoTM Sensor Shield; Raspberry Pi can read ADC values to acquire environmental signals through communication with the Embedded Pi.

Embedded Pi is a Raspberry accessory specially designed by CooCox for Raspberry Pi fans and ArduinoTM enthusiasts. As a bridge between Raspberry and ArduinoTM shields as well as one between ARM Cortex-M3 and ArduinoTM shields, the Embedded Pi integrates and blends the three communities together – Raspberry Pi, ArduinoTM and Cortex-M3. For detailed information, please visit

This article introduces how to add ADC functionality to the Raspberry Pi using the Embedded Pi.
The MCU STM32F103 on the Embedded Pi is capable of acquiring signals. As the controlled terminal, the Embedded Pi receives real-time command of acquiring ADC values from the Raspberry Pi (refer to here for the command frame format), decodes the command, starts the Analog-to-Digital Conversion of specified channels, and returns the conversion results to the Raspberry Pi via UART interface. In this case, the Raspberry Pi realized ADC functionality in cooperation with the Embedded Pi.

Using this principle, the Raspberry Pi outputs real-time resistance value of the linear potentiometer and controls the brightness of the LED in the demo below.

2.Hardware Preparation
2.1 Required Devices

One piece of Raspberry Pi
One piece of Embedded Pi
One linear potentiometer
One 26-pin flat cable
Several Dupont lines

2.2 Hardware Connection

Configure JP2 and JP3 to set the Embedded Pi to ST-Adapter mode (For operation mode configuration, refer to Embedded Pi User Manual).

Connect the Embedded Pi with the Raspberry Pi using the flat cable (In this case, the UART interface of the Raspberry Pi and the Embedded Pi are connected).

Connect the linear potentiometer to the Embedded Pi – Connect the analog input to A0 pin of the Embedded Pi, LED to D3 pin of the Embedded Pi.

Connect the ArduinoTM UART interface with the UART interface of Embedded Pi/Raspberry Pi, as the yellow lines in the figure below (The improved version demo will use I2C communication instead, and this step can be skipped then):[/list]
So finally your kit should look like the figure below:
2.3 Update the firmware of Embedded Pi
Since the firmware is to be cloned from GitHub, you need to install Git on the Raspberry Pi first (For the introduction and usage of Git, see
$sudo apt-get install git-core
$git --version
git version
Clone/Download the firmware to the Embedded Pi after the installation of Git:
$git clone
After the download, enter the directory of 01.AD_Blinky, you will see three folders:
$cd demo/demo/01.AD_Blinky

In the EPI folder are programs for the Embedded Pi; In the RPI folder are programs for the Raspberry Pi.

The UART interface of Raspberry Pi is occupied by the operating system by default for outputting kernel information. The UART0 needs to be configured before being used for communication with the Embedded Pi and downloading the program. Refer to here for the configuration process.
Besides the source code of programs, CooCox also provided the executive file ad.bin under directory “EPI/Bin” to run on the Embedded Pi:

$cd EPI/Bin

Download the ad.bin file to Embedded Pi from Raspberry Pi

Refer to How to program the Embedded Pi with Raspberry Pi, and download the ad.bin file to the Embedded Pi using the ISP utility on the Raspberry Pi.

3.The ADC Control Code on Raspberry Pi
To reuse the existing ArduinoTM shield drivers for Linux on Raspberry Pi Debian system, CooCox modified the open source library arduPi 1.5 and added ADC functionality. Click here to download the customized arduPi library for the Raspberry Pi. You can visit here to learn the origin and interface introduction of the arduPi library. Here we’d focus on the ADC interface CooCox added based on the Embedded Pi.
3.1 arduPi Library
There are two interface functions for the ADC functionality in the arduPi library.
1) Initialization

void ADPi::begin(){



As it is UART interface that is adopted for the communication between the Raspberry Pi and Embedded Pi for now, the initialization of ADC interface is in fact the initialization of the UART interface. Set the baud rate to 115200 to match with the baud rate of Embedded Pi UART interface.

2) Acquire ADC values

int ADPi::GetValue(unsigned char channel){

Below is an example:

// file:main.cpp
//Include arduPi library
#include "arduPi.h"

void setup()
AD.begin(); // ADC Function Initialization

pinMode(3, OUTPUT); // Set sD3 as ouput mode

void loop()
int ulADCTmp, ulpwm;

ulADCTmp = AD. GetValue(0); // Get the adc digital value of channel 0
printf(“ADC: %d (mV)\n”, ulADCTmp*3300/4095); // Print the adc value

delay(100); // Sleep about 100ms

int main (){


return (0);

3.2 Compile and Run

In the arduPi library, the delay() function calls function clock_gettime(), which includes time.h file, thus the gcc compilation parameters should include -lrt. Meanwhile, the two interrupt thread functions attachInterrupt() and detachInterrupt() are in the arduPi, thus the parameter –lpthread should be included too. Compile and run the program:

$ g++ -lrt -lpthread main.cpp arduPi.cpp -o main
$ sudo ./main
Slide the wiper of the linear potentiometer to different positions, the ADC value output in the Raspberry Pi console changes along, and the LED becomes brighter / darker with greater /smaller ADC value.

4.Follow-up Improvement

With UART communication between the Raspberry Pi and Embedded Pi, it’s a bit troublesome to connect extra wires, and inconvenient for other devices to use the UART interface. In the improved version, CooCox will optimize the communication mode and upgrade the firmware of Embedded Pi, using I2C interface which is more flexible for the communication between the Raspberry Pi and Embedded Pi to give full play to Embedded Pi functionalities. Please pay attention to the updates on CooCox website and blog.

Any problem of using the Embedded Pi, please feel free to contact us.

Posts: 8
Joined: Tue Dec 18, 2012 9:53 am

Re: Add ADC Functionality to RPi with EPi

Mon Aug 05, 2013 2:40 am

great job!
It's really convenient and i hope more function such as PWM and compile STM32 program in RPI

Return to “HATs and other add-ons”