RasberryPi – external power supply / charger [Part #1]

I started a new project today for the raspberry pi. The raspberry needs quite not too much power while switched on, so why not using a battery pack as it’s power supply? This battery would help to avoid the raspberry to shut down during a black-out or just make it possible to use the raspberry pi as a mobile server with it’s own power supply.

Unfortunately there is no battery with a 5V DC supply voltage which stays stable on 5V when the battery discharges. So I would need some boost converter to generate a stable 5V voltage for the raspberry pi supply as well as a charger IC to charge the battery.

So there are some requirements to the external power supply:

  • charge circuit to charge the empty battery
  • stable 5V DC output for the raspberry power supply
  • detection of empty battery (state of charge)
  • automatic shut down when battery state is getting to low

There is also a micro Controller for controlling the charger and watching the battery state (voltage level). The MSP430 and the raspberry pi both have an I2C interface for communicating with each other, so there will be not too much afford to implement a software for checking the state of charge of the battery.

Next step will be to start a schematic for the circuit for a prototype of the charger and to order some of the ICs.


Raspberry Pi – I2C eeprom access

The raspberry pi has some GPIOs and Interfaces for external use / communication. One of these interfaces is an I2C bus for communication with external peripherals.

For a simple test on the I2C bus I used an Serial I2C eeprom from ST (M24C02)

To connect the eeprom to the raspberry pi there are four wires required. GND and the 3.3V supply voltage need to be applied as well as the two wires for the data line (SCL_DAT) and the clock line (I2C_CLK). The pin mapping of the raspberry pi is documented in the raspberry schematics which can be downloaded from the raspberry web site. The required pins for connecting the eeprom are shown in the picture below:


The next step is to enable the I2C support on the raspberry side which is disabled as default setting.
To enable this feature the /etc/modprobe.d/raspi-blacklist.conf  must be edited. Just remove or comment out the line blacklist i2c-bcm2708.

Now the two required modules, i2c-bcm2708 and i2c-dev, must be loaded into the kernel (modprobe <MODULE>). To make the modules available after each reboot of the device, both modules can be attached to the /etc/modules file.

Now I2C communication is available. To test the interface the package i2c-tools can be installed via apt-get to get some basic commands to access the eeprom. This includes the commands i2cdetect, i2cdump, i2cget and i2cset.

Lets check if the interface is detecting the eeprom:  sudo i2cdetect -y 0 will scan the i2c bus for peripherals responding (parameter -y causes the i2cdetect command not to use interactive mode, so no additional acknowledge of the command will be required).

 0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: 50 -- -- -- 54 -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

In this case two eeprom with different address configuration are connected to the bus listening to the I2C addresses 0x50 and 0x54. With the i2cget and i2cset commands the content of the eeprom can be edited / read out.


Optical sensor for TI Launchpad

This is a simple optical sensor using an LDR and a LED to classify e.g. the underground like the lego mindstorm optical sensors.

The LDR (R2) and the resistor R1 represent a voltage devider. So the ADC_IN voltage level depends on the light shining on the LDR.  LED1 is only used for some light when the LDR is pointing to the ground an there is not enought light for the LDR. R3 is important for limiting the current through the LED to approx. 33mA.


This ciruit can be placed to a pcb and used for a microcontroller to measure the reflection of the light from the ground. I used this sensor to detect white or black ground for a small car to follow a black line. The ADC_IN signal is processed via the AD-converter of the TI Launchpad. The 10Bit ADC (0…1023) showed a value of approx. 150 with max. light to the sensor and approx. 1000 for no light on the sensor.

This sensor is the first part for a TI MSP430 library which enables the Launchpad to be used like a lego mindstorm controller.

Using the MSP430G2553 uC and Pin P1.4 as the ADC input pin the function int read_sensor_a(void) reads the voltage level on the LDR and returns a 10Bit digit representing the light level.

int read_sensor_a() {
 ADC10 PIN 1.4 READ VALUE range(0...1024) (0V...Vcc)
 int ad_value = 0;
 ADC10CTL1 = INCH_4 + ADC10DIV_3 ; 
 ADC10CTL0 = SREF_0 + ADC10SHT_3 + ADC10ON + ADC10IE; // select reference voltage
 ADC10AE0 |= BIT4; //P1.4 ADC 

 ADC10CTL0 |= ENC + ADC10SC; // start conversion
 ad_value = ADC10MEM;

 ADC10CTL1 &= ~(INCH_4 + ADC10DIV_3); // disable ADC and clear registers
 ADC10AE0 &= ~BIT4;
 ADC10CTL0 &= ~(ENC + ADC10SC);
 return ad_value;


MSP430G2452 PWM example

With the TI  MSP430G2452 you can use the internal timer to generate a PWM modulated output. Not all the IO pins can be used as an timer output for PWM, so you need to check the datasheet for the MSP430 uC for the pin description.

In this example the TimerA0 is running in count-up mode counting up to TACCR0. Every time the TimerA0 counts to TACCR1 the specified output pin is set to high until the counter reaches TACCR0 where the output pin is set to low again.
So with the modification of TACCR0 and TACCR1 you can modify the duty cycle of the PWM signal on the output pin. The duty cycle is the ratio of signal high time to signal low time and equal to TACCR1/TACCR0.
The complete periode time of the PWM signal is set with the TACCR0 value.

To show how different duty cycles effect the output pin, the button on the launchpad is configured as an input for increasing the duty cycle by 10% on each button push.

(This example code is for the msp-gcc4 compiler. If you use a different compiler you need to check the interrupt vector names in your msp430g2452.h file for the correct vector names)

#include <msp430g2452.h>
#include <legacymsp430.h>
 Ratio for this PWM test ist TACCR1/TACCR0 (duty cycle)
 Select low values for both registers for higher sampling frequency
int main(void)


 P1DIR = BIT6 | BIT0 | BIT4;
 P1OUT &= ~(BIT6 | BIT0 | BIT4);

 P1DIR &= ~BIT3;

 P1IE |= BIT3; // P1.3 interrupt enable
 P1IFG &= ~BIT3; // P1.3 IFG cleared
 P1REN |= BIT3; // P1.3 Resistor enable (Pull up or Pull down)

 // set sub-system clock divider to 8

 // set TACCRO register -> count to
 TACCR0 = 100;
 // select TimerA source SMCLK, set mode to up-counting

 // set ouput to Port P1.6
 P1SEL |= BIT6;

 // select timer compare mode 
 // Interrupt called when counter reaches TACCR1
 // set up counter_limit for interrupt TIMER0_A1_VECTOR
 TACCR1 = 0;
// enable all interrupts

 // endless loop 
 for (;;) { 

 return 0;
// This will be called when timer counts to TACCR1.
interrupt(TIMER0_A1_VECTOR) ta1_isr(void)
 // clear interrupt flag
interrupt(PORT1_VECTOR) p1_isr(void) {
 if (P1IN & BIT3) {
 TACCR1 = (TACCR1 +10)%100;
 P1OUT ^= BIT0;
 P1IFG &= ~BIT3; // P1.3 IFG cleared
 P1IES ^= BIT3;


TI pre-programmed sample

The TI Launchpad comes with a preprogrammed MSP430G2553. The sample program uses the ADC to measure the internal temperature and prints the Temperature via serial interface to a connected pc.

There is an application programmed in processing (processing.org)  showing the temperature reading converted to degrees celsius. The code for the UI aswell as for the MSP uC can be found on the TI website (here).

The original code for the MSP uC seems to be not written for the MSP430G2452 processor. Due to I had this one populated to the board I needed some adjustments in the code to get the application working.

Code modifications: 

Line 316: changed to: interrupt (TIMER0_A0_VECTOR) Timer_A(void)
Line 351: changed to: interrupt (TIMER0_A1_VECTOR) ta1_isr(void)

These modifications are nedded when using the MSP430G2452 and the msp-gcc compiler. Some of the names in the MSP430G2452.h header file have changed, so you need to make your code match the new names.

TI Launchpad / Mac programming

Sadly TI does not provide any IDE or programming tools for non Windows platforms. At least there is an free msp430 gcc compiler. So you can program your Launchpad with Mac / Linux if you don’t work with windows.

The compiler and all required tools can be found on http://code.google.com/p/osx-launchpad/
The  kext USB driver in this package did not work for me, but there is a second package on this site (MSP430LPCDC) which has a pretty fine working USB driver instead.

TI Launchpad

It arrived yesterday :) The TI MSP430 Launchpad. The Launchpad is a development kit for the TI MPS430 series microcontroller with low power consumption and up to 16MHz clock speed. The kit can be ordered for less the 5€ and enables the ability to flash the MPS430 series microcontroller via an USB connection.

The package includes one  MSP430G2452 and one MSP430G2553 microcontroller. The MSP430G2553 comes preprogrammed with a demo application showing the how the integrated uC temperature sensor is used to set the brightness of the integrated LED proportional to the internal temperature.

Technical information:
MSP430G2452: 8kB Flash, 256B RAM, 16 GPIO, 1x 16Bit-timer, WDT, BOR, 1x USI (I2C/SPI), 8ch 10-bit ADC, 8ch comparator
MSP430G2553: 16kB Flash, 512B RAM, 16 GPIO, 2x 16Bit-timer, WDT, BOR, 1x USCI (I2C/SPI/UART), 8ch 10-bit ADC, 8ch comparator

so… can’t wait to test this board with some own applications :)

Matlab and WEKA – mat2arff conversion

Features extracted with matlab are stored in .mat files for later analysis within matlab. To test some classifiers for the extracted features with the WEKA toolkit, a machine learning software written in JAVA, the .mat files need to be converted into the .arff file format before using the feature data in WEKA.

For some batch processing of the feature data I implemented a matlab script using the .mat file and some parameters as an input to generate an .arff file which can be read by the WEKA application.

For converting a folder containing .mat feature files the following script reads all the .mat files and attaches them to the output .arff file.

batchFolder_mat2arff( input_path, output_path, relation )

input_path and output_path represent the corresponding path as a string. The output_path variable must include the output filename with the .arff extention. The relation variable can be choosen individually and only represents the type / name of the classification problem.

mat2arff( mat_filename, arff_filename, relation, classes )

mat2arff.m is the main function for the conversion and is also called by the batchFolter_mat2arff.m function. The first two parameter represent the input and output path. The usage of relation is the same as for the batch script. During the first call of this function the possible classes for classification must be known. So the variable classes expects a comma separated string containing all possible classes. If the function mat2arff.m is called more than once for one single .arff output file, the 2nd, 3rd… call of the function only attached additional features to the existing .arff file. If additional classes are required for the classification problem the new classes have to be added manually to the .arff file with some text editing tool.

For the conversion your .mat files need to match some requirements:

.mat file conditions:
%   ------------------------------------------------------------------
%   requires following variables stored inside the .mat file.
%   [string]        AudioType
%   [double matrix] featureMatrix
%   AudioType containing the attribute for the features in this .mat file.
%   featureMatrix containing the feature data as a 2-dim or 3-dim double
%   precision matrix
%   All other variables stored in the .mat file will not be used in this
%   version.

Download: mat2arff | batchFolder_mat2arff

For additional information on the usage of the script files see the comments embedded in the .m files.