A web developer playing with the Intel Galileo part 3: Analog Input

Next in the list is Analog Input. Once again following the arduino template, the Galileo has 5 pins that read a voltage  with a resolution of 12 bits, meaning that we will get a number from 0 to 4095 where 0 is 0 volts and 4095 is 5 volts. This is useful for reading data out of sensors and such.

You can see the analog pins labeled from A0 to A5.

Now, the sources that have guided us so far tells us that reading from the analog is not as easy as setting things on the GPIO and reading the value file. Analog pins need to be set up correctly in the inner circuity and then read trough files in /sys/bus/iio/devices/iio:device0.

Setting the inner circuity is the trickiest part, and we do it so by playing around with the multiplexers (a circuit that is able to be digitally controlled to chose what the input is connected to) and setting their values in such a way that the analog pins would be readable. Each multiplexer is connected to GPIO number and controlled from there, so we set a new variable with maps each analog pin to the multiplexer that controls it.


var analogMuxMapping = {
	"A0": 37,
	"A1": 36,
	"A2": 23,
	"A3": 22,
	"A4": 21,
	"A5": 20
}

In order to set up the multiplexer correctly we need to export the corresponding GPIO number, set it to output mode and set the value to 0 using the methods that we have already used for digital pins. We can easily create a function in the library that does this in steps:


prepareAnalogIn = function(gpio_mux, callback){
	exportGpio(gpio_mux, function(){
		galileo.setGpioOut(gpio_mux, function(){
			galileo.writeGpio(gpio_mux, 0, callback);
		});
	});
}

Once that is done, reading the pins value is simply reading from a file is /sys/bus/iio/devices/iio:device0/. There are two types of files there for every pin, a in_voltage*_raw and a in_voltage*_scale. The first one gives you the raw number between 0 and 4095, while the second gives you a scaled representation of how many volts do that mean. We will limit ourselves to using the raw value, but you can use the scaled one if needed. Also, this time the pins are not mapped out in strange ways, so 0 actually means A0 and 5 actually means A5.

The resulting javascript function looks like this:


galileo.readAnalog = function(analog_nr, callback){
	fs.readFile("/sys/bus/iio/devices/iio:device0/in_voltage" + analog_nr + "_raw", fileOptions, function(err, data) {
		if (err) { console.log("Error reading analog" + analog_nr); }
		var value = data;
		callback(value);
	});
}
Advertisements

An upcoming web developer and android app-maker with an interest on using reliable tech in creative ways, open source projects and start-ups.

Tagged with: , , ,
Posted in developer tips

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: