More data

So I figured that I needed a level shifter for the serial connection as the Leonardo is 5V device and the Pi is 3.3V. While I was shopping at Pimoroni, I had a little look at the sensors section. Oh boy.

I ended up getting an ADXL345 3 axis accelerometer and a BMP180 Pressure sensor.

My first thought at hooking them up was through the Pi, as that supports I2C on two ports but as I tried getting that working, it occurred to me that the Arduino was a much better place. Keeping the data acquisition in one place and the visualisation in another. Sort of in keeping with the MVC model, with the Leonardo being the M and the Pi being the VC.

Temperature

After some messing about, the BMP was actually quite simple to integrate:

  1. Hook up Arduino’s 3.3V to the Vin
  2. Hook up Arduino’s ground to ground
  3. Hook up SCL and SDA to the Leonardo’s dedicated SCL and SDA pins.
  4. Add the Adafruit sensor package to the source

That latter step looks something like this:

  1. git clone the Adafruit_Sensor repo into the Arduino’s sketchbook/libraries folder
  2. git clone the Adafruit_BMP085 repo into the Arduino’s sketchbook/libraries folder (the BMP085 is a previous revision of the BMP180 which is entirely compatible
  3. Add the below code, given example from Adafruit

#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>

...

Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

void setup() {
  ...

  /* Initialise the sensor */
  if(!bmp.begin())
  {
    /* There was a problem detecting the BMP085 ... check your connections */
    Serial.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }

  ...
}

void loop() {
  ... 

  sensors_event_t event;
  bmp.getEvent(&event);
  /* Display the results (barometric pressure is measure in hPa) */
  if (event.pressure)
  {
     float temperature;
     bmp.getTemperature(&temperature);
    /* Display atmospheric pressure in hPa */
    Serial.print("Pressure: "); Serial.print(event.pressure); Serial.print(" hPa;");
    Serial.print(" Temp: "); Serial.print(temperature); Serial.println(" C");
  }

  ...
}


This very happily prints the current pressure and temperature in my room:

Pressure: 1017.11 hPa; Temp: 25.30 C

The barometric pressure isn’t all that interesting to me, it’s the temperature I care about more. Really I could do with two sensors, inside and outside temperature but this’ll do for now. One of my concerns with this would be from the source that it’s doing a lot of stuff that I’m not necessarily interested in. I’ll take in the wise words from Mr Knuth in that premature optimization is the root of all evil, so when I’ve finished with all the data acquisition I’ll do some profiling to see if it’s actually an issue.

Accelerometer

As for the accelerometer, well, that’s supposed to be an I2C and SPI device. The Leonardo’s dedicated I2C pins are being taken by the temperature sensor, so that leaves the SPI. Luckily the Leonardo also has some dedicated SPI in the form of the ICSP header.

According to the reference the Leonardno has ICSP-4 as MOSI (isn’t that how Japanese answer the phone?), ICSP-1 as MISO (Oriental prince in the land of SOUP!) and ICSP-3 as SCK with no SS.

Very helpfully (he said sarcastically) the ADXL is labelled with Vin, 3.3V, CS, INT1, INT2, SDO, SDA and SCL. Which are I2C labels. There is a tutorial at sparkfun how to hook up the ADXL to an Arduino with SPI, but it still uses the I2C pins. For most Arduino boards these pins double up for SPI but the Leonardo is slightly different in that the SPI is only present on the ICSP header. That means I don’t really know which pins go where.

Lucky the people at Adafruit also have a sensor library for the ADXL which supports both I2C and SPI modes, depending on which constructor is used. Reading the code, to enable SPI you have to specify which pins to use, which should be straightforward with the Leonardo. However still leaves the matter as to where the MOSI, MISO and SCK pins go. There’s one chap(ette) on the Sparkfun tutorial mentioned above that the following should apply:

10 -> CS
MO(MISO) -> SDA
MI(MOSI) -> SDO
SCK -> SCL

Which is ok for the 4 pin SPI connection, but the header on the Leonardo is like this:

To my reckoning that means I only have 3 pins available. The CS is the question here. I don’t want to sacrifice one of the other pins for that if I can.

I’ll have to do a bit more reading before I proceed with this.

Latest reading on pressure & temperature

Pressure: 1017.27 hPa; Temp: 25.60 C
Advertisements

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: