Saturday, 22 August 2015

Getting the BMP085/BMP180 pressure sensor working with EMFCamp's Tilda MKe badge

So I've been looking at making something useful out of my Tilda MKe badge from last years EMF Camp for some time now. With its nice (backlit) graphical LCD, I decided it'd be excellent to use as a temperature and atmospheric pressure display for my workshop at work. When you're calibrating these or these or these, you need to compensate for both to get an accurate distance measurement.

I bought a cheap as BMP180 sensor (it's a pin & software compatible updated version of the BMP085) from eBay for £1.19 and hooked it up to the first working Arduino I could find (which was about the third one I tried - really need to fix the others) and got out glorious pressure and temperature data over the serial port with the help of the Adafruit library.

Great! Getting this going on the Tilda should be easy as. I knew from a bit of tinkering with the badge I did on site over the EMF Camp weekend that they were using an RTOS and they'd helpfully added a 'Hello World' app in the firmware for hacking purposes. I quickly modified the Hello World app to display the temperature.

It didn't work.

Huh. Somewhere in the back of my mind, I recalled that the Arduino Due (which the Tilda is based on) has two I2C interfaces. My first thought, confirmed by a quick look at the schematic, was that perhaps the Tilda's SDA and SCL pins that were brought out on the Arduino header weren't connected to the first I2C interface, which is what would be referred to when you normally use the wire library.

No worries. I just had to modify the Adafruit library to use wire1 instead of wire. A bit of find/replace and I was getting temperature data displayed on the LCD. Yay.

From here, it should just be a case of adding in the call to read and display the pressure.

It didn't work.

But not in the way I had expected. I was geting some pressure data, but it was way, way too low. Like it thought I was up at 40,000m kind of atmospheric pressure.

I spent a bit of time futzing about with various things. I knew it had to be related to the Tilda somehow so I looked into the RTOS and delved deep into the BMP085 datasheet. 

To measure the pressure with the BMP085 takes three steps:
  • Firstly, each unit is individually calibrated at manufacture. The various calibration registers need to be read and their values saved for later calculations.
  • The pressure value is temperature sensitive as well, so the (uncompensated) temperature needs to be read from the chip and the true temperature calculated with the calibration data.
  • Lastly, read the uncompensated pressure value and, with the aid of the calibration data and the compensated temperature, you can go through a 15-step calculation to arrive at the correct pressure.
I read in the datasheet that after requesting the pressure the chip can take up to 25ms for the ADC to convert the reading. I had a look in the Adafruit library:
  if (oversampling == BMP085_ULTRALOWPOWER)
  else if (oversampling == BMP085_STANDARD)
  else if (oversampling == BMP085_HIGHRES)
I knew that doing this kind of blocking delay would stop background tasks in the RTOS from working so I changed them all to Tilda::delay() as suggested, and crossed my fingers.

It didn't work. I was getting a little bit annoyed at this point, so decided to take a little several week break.

When I came back to it I continued looking at the way the Tilda was reading in the pressure data from the BMP085. Since the temperature was being calculated correctly it didn't point to an error in the calibration data or temperature reading.

To confirm that it wasn't a hardware problem with my Tilda, I decided to try programming it as an Arduino (ie, without the RTOS). Unfortunately, if you try this in the latest Arduino software (currently 1.6.4) you'll get a heap of errors like this:
/home/peter/Downloads/Mk2-Firmware-master/hardware/emfcamp/sam/variants/tilda_mke_v0.333/variant.cpp:342:55: note: candidates are:
In file included from /home/peter/Downloads/Mk2-Firmware-master/hardware/emfcamp/sam/variants/tilda_mke_v0.333/variant.h:43:0,
                 from /home/peter/Downloads/Mk2-Firmware-master/hardware/emfcamp/sam/variants/tilda_mke_v0.333/variant.cpp:24:
/home/peter/.arduino15/packages/arduino/hardware/sam/1.6.4/cores/arduino/UARTClass.h:45:5: note: UARTClass::UARTClass(Uart*, IRQn_Type, uint32_t, RingBuffer*, RingBuffer*)
The easiest solution was to use an older version of Arduino. I had a guess at which one they'd used to write the Tilda software in the first place and picked version 1.5.7, which worked fine.

I used the test sketch from the Adafruit BMP085 library (after modifying it to use wire1 again) and I was getting the correct temperature and pressure data out of the serial port!

Now I knew it wasn't a hardware problem, I needed to see the communication between the Tilda and the BMP180, and I had exactly the tool for the job, having recently acquired a Rigol DS1054Z Oscilloscope.

I fired it up and was quickly able to probe the SCL (clock, top in yellow) and SDA (data, bottom in blue)

Zooming in on the last burst, we can use the DS1054's decode function to see what all these blips actually correspond to:

This is with the Tilda in Arduino mode - the data being read is 0xA26C20 (decimal 10644512). 

Here's with the Tilda back running my HelloWorld app in the RTOS. The data read here is 0xA277C0 (decimal 10647488). The difference is only about 0.03%. Surely not enough to account for my massive error.

Obviously I was now going to have to delve into the calculations. Here's the page in the datasheet that describes the procedure:

Err... that looks complicated. I ended up making a spreadsheet to help. I plugged both uncorrected pressure values into the spreadsheet and got consistent pressure values out. Well fuck. I was pretty sure now that the bug was somewhere deep in the RTOS and I really didn't think I was going to be able to find it. Unless.... 

I'd been working on the assumption that the calibration data was being read ok because the temperature was being calculated correctly. However, after working through the calculation in my spreadsheet I knew that most of the calibration data doesn't get used until the pressure calculation.

I got the Tilda to spit out the calibration data in both Arduino mode and the RTOS and lo and behold, the values for AC1 and AC5 differed markedly. I guessed that some background task in the RTOS was interfering with the read. I had a stab at fixing it by adding in a delay into the library:

  if (read8(0xD0) != 0x55) return false;

  /* read calibration data */
  ac1 = read16(BMP085_CAL_AC1);
  ac2 = read16(BMP085_CAL_AC2);
  ac3 = read16(BMP085_CAL_AC3);
  ac4 = read16(BMP085_CAL_AC4);
  ac5 = read16(BMP085_CAL_AC5);
  ac6 = read16(BMP085_CAL_AC6);
Which worked perfectly! All of that time and frustration was fixed by a 5ms delay in the right place, apparently. 

Niiiiiiiice! I'm not exactly convinced the temperature is 100% correct. It seems to be a couple of degrees warmer than I think it should be. The pressure looks to be correct though. 

Next time: Making it into a usable barometer.

Monday, 2 July 2012

PCB Making

I've been making a few PCBs recently with the toner transfer method with mixed results. This one turned out really well though. It's just a little adapter for changing the surface-mount connector for a 2*20 character LCD display to something a bit more breadboard friendly. Still haven't got around to actually using it though..

Wednesday, 14 March 2012

Big Muff clone completed

I've finished my clone of the well-known Big Muff distortion pedal. I painted it with some grey primer and then with a design of Cardiff Bay as I have a vague notion of making myself a range of Cardiff-themed effect pedals. Total cost in parts was around £16-17 and a lot of that is because I bought the extortionately expensive switch from Maplin instead of pretty much anywhere else.

It sounds quite a lot like the original as far as I can tell. It is REALLY LOUD, and is unfortunately not very useful for playing in our apartment as I get the feeling I must be annoying the hell out of the neighbours, even at low volumes.

Next up will be a Small Clone clone. A clone2 I guess. It's a chorus pedal that was used by Kurt Cobain on Nevermind apparently. I've ordered up the parts from far-flung ebay sellers across East Asia, so they'll take at least a couple of weeks to get here, which gives me plenty of time to change my mind and start and abandon another several projects.

Sunday, 11 March 2012

Big Muff clone

 This weekend I've been down at Cardiff Hackspace building a clone of the well known Big Muff distortion pedal, first getting it working on a solderless breadboard:

Then I built it on some copper stripboard, but for some reason couldn't get it working. After checking, rechecking and re-rechecking, I finally spotted the problem:

See the problem? No?

There it goes - at one of the points where I'd used a drill bit to break the copper track I hadn't quite broken it completely. Those holes are 2.54mm apart so the bit of copper that was left couldn't have been much more than 0.2mm wide, but it was enough for electrons to squeeze through and stop it producing any sound.

Once that and several other problems were sorted, I brought it home and tried it out. It doesn't sound exactly like the original, but it has a nice tone with loads of gain. Hopefully this week I'll put it in a nice enclosure.

Saturday, 3 March 2012

Soldering iron power LED

 For xmas a couple of years ago my parents bought me a nice temperature-controlled soldering station which I've been using to build all my half-completed projects. It works great, but it has one small problem: the LCD display isn't backlit so, at a glance, it isn't obvious that the power is on. This isn't normally a problem but if I'm working on something that means I'm soldering something and then going back and forward to the computer to program an Arduino or whatever, it's possible to forget the soldering iron is on.

And then go to bed.

And then go to work.

And then come home and find out that there's been a 50W desk-mounted heater on for 24 hours and somehow we still have a home and not a smouldering ruin.

The solution to this problem is trivially easy - mount an LED on the front to make it obvious that the iron's on. First thing we need to do is open up the soldering iron and find a power source to hook our LED up to.

 This is the main PCB for the iron, and at the top left is an LM7805 linear voltage regulator that supplies the 5v to power the control circuity. Perfect and has long leads that we can solder to. If you're looking at the front of the LM7805, the three leads are Vin - GND - Vout.

 A quick check with the multimeter to confirm. 

I come to a small problem here - how am I going to solder on the inside of my soldering iron? I briefly consider using the soldering iron itself and then remember that I have a butane powered soldering iron for just these sorts of occasions:

I dig out a 5mm red LED and find my LED calculator to work out what current limiting resistor I need. Looks like 150 ohms will do the trick, which is one of the several values I have. 10 minutes and some dremel action later and we have this:

I should have used some heatshrink, but I couldn't find any. It should be ok like this though. The LED fits in pretty snugly but for peace of mind I might stick some hot glue on the back to hold it in. But does it work...?

Power off

Power on!
Success! And all done before breakfast on a Saturday. Quite good fun in the end, and if it stops me from accidentally burning down the house, well that's just a bonus.

Wednesday, 29 February 2012

Skittle sorting

Extremely important Skittle sorting prior to making Skittle vodka, which was amazingly tasty and alarmingly drinkable. Especially the orange flavour. We passed the cheap and nasty vodka through a Brita charcoal filter before infusing it with the Skittles, which I think made a bit of a difference. It certainly made a difference to our filtered water for the next couple of weeks as it had a bit of a vodka flavour until we got around to changing the filter.


This is a nice 20MHz analog scope that was donated by UWIC to Cardiff Hackspace. It's awesomely retro but works great. I don't know how I ever managed to do any electronics without access to an oscilloscope. I keep meaning to use it to display some graphics. I even got around to getting some DACs (Digital to Analog Converters):

Yeah, soldering that onto the breakout board was quite tricky. The main body of the chip (the black bit) is only 1.75 by 1.3mm big. I haven't got around to doing any more than that, so it's in the pile of ideas and half-completed projects.