Interfacing Raspberry Pi and Microchip MCP3304 SPI ADC

Alright. So a lot of people on blogs and forums have been busy interfacing the MCP 3008 DAC to the Raspberry Pi.

But when I came to need one, RS Components, where i do most almost all of my shopping for components, couldn’t supply one, I had to buy another one.

Still cheap, the MCP3304, but with some minor differences. It can be configured to take both single ended and balanced input, and it has 13 bits (12 unsigned), rather than the 10 positive signed bits the MCP3008 offers.

I used Python as language and adjusted some code i found on this blog: http://jeremyblythe.blogspot.dk/2012/09/raspberry-pi-hardware-spi-analog-inputs.html

The Python code:
SPI_MCP3308.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
# mcp3008_lm35.py - read an LM35 on CH0 of an MCP3008 on a Raspberry Pi
# mostly nicked from
#  http://jeremyblythe.blogspot.ca/2012/09/raspberry-pi-hardware- spi-analog-inp$
# Changed to work w. MCP3308 by Kim H. Rasmussen, June 2013
import spidev
import time

spi = spidev.SpiDev()
spi.open(0, 0)

def readadc(adcnum):

 # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
 if adcnum > 7 or adcnum < 0:
    return -1

 # Frame format: 0000 1SCC | C000 000 | 000 000
 r = spi.xfer2([((adcnum & 6) >> 1)+12 , (adcnum & 1) << 7, 0])
 adcout = ((r[1] & 15) << 8) + r[2]
 return adcout

while True:

   # Read from ADC channels and convert the bits read into the voltage
   ch1 = (readadc(1) * 3.3) / 4095
   ch2 = (readadc(2) * 3.3) / 4095
   # Divisor changed from 1023 to 4095, due to 4 more bits
   # Print the stuff just read
   print ('Voltages: ', ch1, ' ', ch2)

   time.sleep(1)

Prerequisites:

Install Python development package:

sudo apt-get update && sudo apt-get install python-dev

Enable SPI to be loaded into the kernel on boot:

sudo raspi-config

And enable SPI on boot in The advanced menu <8>.

Install spi-dev via a terminal:

git clone git://github.com/doceme/py-spidev

cd py-spidev/

sudo python setup.py install

Raspberry Pi with a static IP

How to configure your Raspberry Pi with a static IP:

Simple:

Edit “/etc/network/interfaces” and change:

iface eth0 inet dhcp

to

 iface eth0 inet static
 address 192.168.0.9
 netmask 255.255.255.0
 network 192.168.0.0
 broadcast 192.168.0.255
 gateway 192.168.0.1

The easiest way to change the file is with the SD card still plugged into your PC and then write “sudo nautilus” in the terminal, navigate to the file, edit and save it.

Remember to change the addresses to suit your needs. Mine is 192.168.0.9 because my router is set to hand out dynamic IP’s from the range 192.168.0.10, so the addresses below are reserved for static IP devices, such as servers, printers and embedded units.

Java “Hello World!” Genetic Algorithm (GA).

Just finished a Java application that evolves the phrase “Hello World!”, or any other arbitrary phrase for that matter.

I was to some extend inspired by this post that I found, by a guy who did the same thing in C++.

Using his parameters for a start, the application is able to evolve the phrase “Hello World!” within 70-80 epochs using these settings/methodology:

  • Population size: 1024
  • Fixed chromosome lenght (same as the target phrase: “Hello World!”)
  • 10% elite
  • 25% chance of mutation on non-elite members of the population
  • Pair-bred crossover method, using a single point crossover, producing two children for each set of parents

The ever so important fitness function looks like this:

/* Calculate fitness */
 for(int i=0;i<population.length;i++){
    population[i].fitness = 0;
       for(int j=0;j<population[i].s.length();j++){
       population[i].fitness += Math.abs((int)(target.charAt(j))-(int)(population[i].s.charAt(j)));
    }
 }

The source code can be found here: http://www.havnemark.dk/misc/GAHelloWorld.zip

It is a netbeans project, but should easily import to Eclipse. And the project only consists of two classes, so adapting it to any other IDE should be simple.

Sample run, generates this output:

run:
Now running Java Hello World Genetic Algorithm.
Single threaded application, written by:
Kim H. Rasmussen - Uni. Southern Denmark, April 2012

Iteration: 0 Best Fitness: 530 Text: (j]$=hh15L>k
Iteration: 1 Best Fitness: 162 Text: VQnc^=Qr`siA
Iteration: 3 Best Fitness: 127 Text: NIo9n!Eptnr!
Iteration: 8 Best Fitness: 113 Text: :om_r/VawVX$
Iteration: 10 Best Fitness: 108 Text: BcnYf$Ife}`*
Iteration: 11 Best Fitness: 96 Text: Kmrch4JinkU%
Iteration: 13 Best Fitness: 92 Text: >f|t]!UoseV/
Iteration: 15 Best Fitness: 87 Text: Hl`Uu!Yid^d#
Iteration: 16 Best Fitness: 84 Text: Bfigz(Mid^i
Iteration: 17 Best Fitness: 71 Text: Hgnih%_xim+
Iteration: 19 Best Fitness: 70 Text: Sq~mx!Soovd
Iteration: 21 Best Fitness: 59 Text: Bpfmz&Tooug!
Iteration: 22 Best Fitness: 56 Text: C`dqm!^g~lb"
Iteration: 23 Best Fitness: 55 Text: Iqtlg#Koun^!
Iteration: 26 Best Fitness: 51 Text: Kgfp]!Rrnlb$
Iteration: 28 Best Fitness: 49 Text: FUelo!Koun^!
Iteration: 30 Best Fitness: 44 Text: Dihjk#\pmug!
Iteration: 31 Best Fitness: 30 Text: Ejeoo"Soqnb"
Iteration: 33 Best Fitness: 27 Text: Bfigm!Vlsmg!
Iteration: 43 Best Fitness: 25 Text: Ealgo"Ypnmd$
Iteration: 46 Best Fitness: 23 Text: Hikkl$Tkplc!
Iteration: 48 Best Fitness: 21 Text: Hglgo"Ylpkc$
Iteration: 49 Best Fitness: 18 Text: Hglni!Voqne#
Iteration: 51 Best Fitness: 16 Text: Hgloo"Soqke#
Iteration: 52 Best Fitness: 15 Text: Gfkir!Torkd
Iteration: 56 Best Fitness: 14 Text: Hfkmn"Upsmf
Iteration: 57 Best Fitness: 13 Text: Hglno"Tpsle"
Iteration: 59 Best Fitness: 11 Text: Hflmo"[mrmd!
Iteration: 64 Best Fitness: 9 Text: Hflmo!Tpsle!
Iteration: 65 Best Fitness: 8 Text: Hflmo"Vnqlc!
Iteration: 70 Best Fitness: 7 Text: Hfkmo Uoslc!
Iteration: 74 Best Fitness: 5 Text: Helno!Vosld!
Iteration: 85 Best Fitness: 4 Text: Helmo Uoqld!
Iteration: 88 Best Fitness: 2 Text: Hello Voqld!
Iteration: 95 Best Fitness: 1 Text: Hello Vorld!
Best Fitness: 0 Text: Hello World!

Solution found!
BUILD SUCCESSFUL (total time: 0 seconds)