Accelerometer or Hall Switch

Anyone have some example code they would like to share to get me up and running with either the accelerometer or hall switch on the Hannah board?  If you are really feeling helpful both would be great!

I don't even know where to start.  I usually need to break something a few times to learn how to make it work.

 

Thanks in advance!

I’ll be posting some soon (tomorrow hopefully).


Rob
Fen Consultants, UK

Thanks. I’m also interested in hall sensor and accelerometer.

plus one

This code is obsolete, but it’s helped me a bit in getting things going on the Hannah.


http://devwiki.electricimp.com/doku.php?id=examplecode

thanks

Hall sensor example is online here:


http://devwiki.electricimp.com/doku.php?id=hallsensor

I'll add accelerometer tomorrow, and hopefully most of the remaining Hannah peripherals too.

Rob
Fen Consultants, UK

Thanks a lot Rob. The Hall sensor works pretty well.

This thread is a year old, but I was wondering if a code sample showing basic use of the accelerometer is available.

Still haven’t found it. Dimitri from Smartmaker and Hugo have been looking for Rev3. RGB sensor? Thanks for pointing out the hall sensor code! Is it the same from Rev2 to Rev3?

I saw a lot of people asking for the Rev3 RGB sensor with nothing coming out so I started working on it.

I have coded all the basic functionality required including calibrating, reading color sensors, color offsets and the rest. But where I have come completely unstuck is in try trying to write a value 0x00 to register 0x00 which is part of the most basic register addressing required for the ADJD-S311-CR999 RGB Digital Colour Sensor.

@hugo or @electricimp, am I missing something? the basic byte reading and writing routines (all working with the sx1509, tmp112, lis3dh and everything else) just fail to work with the ADJD?

Is there some trick to building a string to write value 0x00 to register 0x00 at subaddress 0xe8 at port i2c89?

Andre

sorry, values in last sentence should be 0x00, 0x00 and 0xe8

OK, third time lucky “0x00”, “0x00” and “0xE8”

Please post your code and let Hugo or someone look at it?
Sounds like you’re very close, I’m so excited!

Yeah, post your code so that (a) we can check it’s doing the right thing (sounds like it is) and (b) someone else can try it on their hannah in case you have a bad ADJD, which would also explain it!

Below is a striped down version of the original code. If I can get past the i2c.read errors right at the start then I can make the corrections in the full version of the code…

// RGB sensor code, stripped to basics, to get initialization working…
// I2C channel base adddreses
const i2c_ioexp = 0x7C; const i2c_als = 0xE8; // adjusted to 8 bit values

// Initialise the imp hardware
hardware.configure(I2C_89);
local i2c = hardware.i2c89;

// ------------------------------------------------ rgbdcs register definitions -----------------------------------------------------
const rgbdcsRED = 0; const rgbdcsGREEN = 1; const rgbdcsBLUE = 2; const rgbdcsCLEAR = 3;
const rgbdcsControl = 0x00; const rgbdcsConfig = 0x01;
const rgbdcsCapRed = 0x06; // -? 0x09 (x4) for Red, Green, Blue & Clear
const rgbdcsIntRedLo = 0x0A; const rgbdcsIntRedHi = 0x0B; // -> 0x11 (x4) for Red, Green, Blue & Clear
const rgbdcsDatRedLo = 0x40; const rgbdcsDatRedHi = 0x41; // -> 0x47 (x4) for Red, Green, Blue & Clear
const rgbdcsOffsetRed = 0x48; // -> 0x4B (x4) for Red, Green, Blue & Clear

imp.configure(“RGB Sensor (stripped)”, [], []) ;

function InitRGBSensor()
{ local rgbdcsCapas = null, rgbdcsInteg = null, status = null, data = null, subaddr = null, capas = null, color = null;
// Start by forcing a RESET on the ADJD-S311-CR999 (through RESET from SX1509 on Hannah)
i2c.write(i2c_ioexp,"\x7C\x12"); i2c.write(i2c_ioexp,"\x7C\x34"); // consecutive 0x12 and 0x34 to RegReset (0x7C)
// Now cycle through colors from RED thru CLEAR
for (color = rgbdcsRED; color <= rgbdcsCLEAR; color++ )
{ // Now cycle thru all Cap values 0x00 -> 0x0F
for (capas = 0; capas <= 0x0F; capas++ )
{ // Write the Cap register (for a color) with the current Cap value
i2c.write(i2c_als, format("%c%c", rgbdcsCapRed + color, capas));
// Write the Intensity register with my default value (2048 or 0x0800)
i2c.write(i2c_als, format("%c%c%c", rgbdcsIntRedLo + (color * 2), 0x00, 0x08));
i2c.write(i2c_als, “\x00\x01”); // Tell ConTRoL reg we want to read sensor (0x01)
imp.sleep(0.200); // Wait for conversion to finish
status = i2c.read(i2c_als, “\x00”, 1); // Check the control register to be ready 0x00
if (status == null)
server.log("[ERROR] rgbdcs: ConTRoL register read error");
// Now try to read the sensor
data = i2c.read(i2c_als, format("%c",rgbdcsDatRedLo + (color * 2)), 2); // Read actual raw Lo&Hi values
if (data == null)
server.log("[ERROR] rgbdcs: Data Register read error");
local sensorVal = ((data[1] & 0x0F) << 4) + data[0];
if (sensorVal <= 1000) // WOW, we are below the threshold
{ rgbdcsCapas[color] = capas;
capas = 0x0F;
}
}
if ((capas > 0x0F) && (sensorVal > 1000)) server.log(format(“Calibration Error Cap=%d Col=%d Val=%d”,capas, color, sensorVal));
}
server.log(format("[DEBUG] Capas R=%d G=%d B=%d C=%d", rgbdcsCapas[0], rgbdcsCapas[1], rgbdcsCapas[2], rgbdcsCapas[3]));
}

function ReadRGBSensor(color)
{ i2c.write(i2c_als, “\x00\x01”);
imp.sleep(0.050); // Wait for conversion to finish
local data = i2c.read(i2c_als, format("%c",rgbdcsControl), 1);
if (data == null)
{ server.log("[ERROR] rgbdcs: error status from ConTRoL (Sensor) read request"); return (ERROR); }
local byteLo = i2c.read(i2c_als, format("%c",rgbdcsDatRedLo+(color < 1)), 1); // Read actual raw LO value
local byteHi = i2c.read(i2c_als, format("%c",rgbdcsDatRedHi+(color < 1)), 1); // Read actual raw HI value
return (((byteHi & 0x0F) << 4) + byteLo); // return unsigned 12 bit value
}

function MainLoop()
{
local tempRED = ReadRGBSensor(rgbdcsRED); local tempGREEN = ReadRGBSensor(rgbdcsGREEN);
local tempBLUE = ReadRGBSensor(rgbdcsGREEN); local tempCLEAR = ReadRGBSensor(rgbdcsCLEAR);
local Message = format(“als: R=%d G=%d B=%d C=%d”, tempRED, tempGREEN, tempBLUE, tempCLEAR);
server.log(Message);
imp.wakeup(5, MainLoop); // continue sleep, wake up every xx seconds
}

InitRGBSensor();
MainLoop();

Should I just run it as is? Which lines cause the errors?

@ammaree I’d be happy to help if I can. Your code is giving me the following errors (I had to remove some
s first):

2013-09-22 20:08:53 UTC-4: [Device] [ERROR] rgbdcs: ConTRoL register read error
2013-09-22 20:08:53 UTC-4: [Device] [ERROR] rgbdcs: Data Register read error
2013-09-22 20:08:53 UTC-4: [Device] ERROR: the index ‘1’ does not exist
2013-09-22 20:08:53 UTC-4: [Device] ERROR: at InitRGBSensor:39
2013-09-22 20:08:53 UTC-4: [Device] ERROR: from main:70

It looks like, first, we’re not getting a status from this line:
status = i2c.read(i2c_als, “\x00”, 1);

And second, because we’re not getting a status, the device isn’t ready, and we’re getting errors with this:
data = i2c.read(i2c_als, format("%c",rgbdcsDatRedLo + (color * 2)), 2);

Seems like the symptoms you are seeing is exactly the same as what i described earlier in the post.

@Hugo, if you can have a look and let us know if we re missing something. It seems 2 of us are getting the same results so unlikely to be my hardware…

This seems to work for me. I don’t know if the values are meaningful but it runs and the values change over time.

I am unsure of what the (color < 1) was supposed to be in the lines below … so I removed it.

local byteLo = i2c.read(i2c_als, format("%c",rgbdcsDatRedLo+(color < 1)), 1); // Read actual raw LO value
local byteHi = i2c.read(i2c_als, format("%c",rgbdcsDatRedHi+(color < 1)), 1); // Read actual raw HI value

(Sorry, about my previous comment … I was way off base.)