Monday, 18 April 2016

Arduino-PID-Library online Trainer

PID seems to be a rather magical thing. I ported the Arduino-PID-Library to javascript so you can play with the constants in a simulator and and see the results immediately. Hopefully this will make people less scared about the tuning process.

In this simulator, you are Lara Croft driving a car. Using the sliders below you have to choose Kp, Ki and Kd to follow the green line as closely as possible, that is without overshooting or oscillating (so, not like now).

This PID controls the accelerator/decelerator of a car. There is some drag and the vehicle has a certain mass.

The X axis is time and the Y axis is the position of the car. Make sure she is at the right position at the right time

Can you help her? Good luck!


PID's output (accelerator):


  • The car is simulated by using the following equations:
    • x(t) = v*t + 0.5*a*t^2
    • v(t) = a*t
  • The PID code is based in the Arduino PID Library by Brett Beauregard.

Lessons learnt:

There are 2 types of processes.
  • Processes that keep their last state (like in the level 1, a car on a flat road). These are called integrating processes, they don't need a Ki and the PID won't overshoot
  • Processes that feel an external force (car on a slope, something cooling down). These are called self regulating processes, they do need a Ki and they will overshoot when the external force changes

Credits go to Brett Beauregard for his excellent library and his great explanations in the diy-pid-control forum

Saturday, 20 February 2016

Monitor your fridge temperature

Ever wondered how efficient your fridge is?

Mine with a set point of 9 degrees needs to cool for 45 minutes(compressor on) and seems to keep the cold for another 45 minutes(compressor off). The temperature seems to oscillate ±1.5 degrees as you see in the pic.

I wonder how well other people's fridges perform. Should I change it? I wish everyone was posting their data! 

Here is a picture of my setup:

I am using a ESP8266 connected to an LM75 sensor. I am using a 5000mAh 3.7V battery connected through a step down voltage regulator to get 3.3V.

The device has been running for 37 hours thanks to using deep sleep. Every minute the ESP8266 was waking up, measuring the temperature, sending it to a server and then back to sleep. 

The consumption per wake up is: 5000mAh/37h/60 = 2.25mA

Just for laughs I'm going to elaborate on this server, it is just an old 'HTC Desire S' cellphone that is running Debian linux. This cellphone is now my cloud and it is running 24/7, it has installed PHP, nginx, mysql, samba, minidlna, ssh, avahi, and OpenVPN so I can connect remotely and have full access to everything. 

In order the deep sleep function I had to connect the XPD_DCDC with the reset pin, if you zoom into the ESP8266 you'll see the dirty hack (credits go to for the idea)

I find rather interesting what happened when the battery was running out of juice. The ESP seemed to work properly but the LM75 sensor was becoming more and more noisy

One would expect that the ESP would be the first one to notice the lack of power! The time stamps are generated in the server so this seems to imply that the ESP was doing its job at the right intervals.

If people show enough interest I will tidy up the code and upload it to github.

Sunday, 13 September 2015

Hacking an electronic price display, success!!

Somehow I found one of this tags (don't ask) and decided to hack it. The display is WF021-01 and you can find eink's datasheet here. Unfortunately this doc it in incomplete and it doesn't show the list of commands to control the display. Luckily I found many similar datasheets from other similar eink displays and noticed that many of those commands are the same across devices. I tried some of those commands and found the ones that work with my display. Tadaaa!

This is how the back of the device looks like:

I cant tell much about the epoxy blob, luckily the PCB has a bunch test pads that can be used to drive the display (I had to cut the traces to stop the main processor from interfering with my data):

This is what each test pad is for:

  • tp11: bs1(set this oen to ground so you can use the 3 wire interface a)
  • tp10: res
  • tp8 : d/c  (no need to drive this one if you set tp11 to ground)
  • tp7: busy
  • tp6: cs
  • tp5: d0
  • tp4: d1
  • tp1: vddi
  • tp18: gnd 

I am using an arduino to talk to the eink display, I'll post the code to drive it as soon as I clean it up a bit. Enjoy!

Tuesday, 23 June 2015

Simple nodemcu module to drive a nokia LCD (PCD8544)

I desperately wanted to hook a Nokia5110 LCD to my ESP8266/nodemcu. I wrote a LUA module but it was slow and it was taking too much memory, mainly because the font data needs to stay in the precious RAM.

It looks like anything I do ends up taking too much memory, so the way to go is to write modules in c and then invoke them from LUA. I am very pleased with the results, so here it is for you to use: Download (just for fun the lua module and the image are also included)

You can use it this way:
  • pcd.init()
  • pcd.locate(0,0)
  • pcd.print("Hello world!")
And if you want to display images you can do it this way:
  • pcd.init() 
  •"image.bin", "r") 
  • pcd.printbin( 
  • file.close()
I already sent a pullrequest to the nodemcu guys, it may not get accepted as the pins needed are hardcoded :/ 

Saturday, 5 October 2013

Software defined FM radio transmitter (Arduino + AD9850)

This video shows it all, take it away!

The frequency is generated by the AD9850 and the modulation is done by the arduino

The full code is here:

The FM band channels are centered at certain frequency, in our case 90Mhz, any slight deviation of that freq (by a max of +/- 75Khz) will move the membrane of the speaker backward or forward, here is the bit of code that implements that. And this is the (unoptimized) modulation routine.
void playTone(unsigned int note, unsigned int duration ) 
  const double freq = 90 * 1e6; // transmitting at 90Mhz

  // play silence 
  if ( note == 0)
  unsigned long currentMillis = millis() + duration;
  while( millis()< currentMillis)
      ad.setfreq(freq + 75e3/2); // speaker's membrane backward
      ad.setfreq(freq - 75e3/2); // speaker's membrane forward

  ad.setfreq(freq );

Credits go to:
Brett Hagman for the RTTTL tune player
Poul-Henning Kamp for the AD9850 lib

Friday, 5 July 2013

Minimal ECG using an Arduino and Xoscillo

First off, the picture :)

To display the wave I am using XOSCILLO, a very cool and open source tool (which I wrote :P) that converts your Arduino into an oscilloscope.

Circuit diagram

Here is the scheme, I am using the typical instrumental amplifier(ins amp) and then another op amp to get some more gain. An ins-amp is like an op-amps but with a huge CMRR, I am using a AD8221 which comes in a tiny small package, it's lead pitch is only 0.65mm, if you want to know what I did to create a simple breakout board please follow this link.

I'm using a single supply for the amps that comes from the arduino 5V pin, and I am creating a buffered virtual ground, this is really important.

Here is, a bit dark, a pic of the setup in the breadboard.

Tricks of the trade:

This is what makes the difference between a working ECG and a non working one :)

The main problem is that the device is so sensitive that it get interference from almost everywhere, specially the mains hum at 50hz). As you can see I got a clean signal, and I am not using any kind of serious filtering, note that xoscillo has a low pass filter and I am not even using it, so what are the tricks?

  1. Run it from a laptop and unplug the charger!
  2. Don't use a second monitor, the HDMI port will cause all sorts of high frequency interferences
  3. Get about 1 meter away from the laptop
  4. Buy some serious ECG electrodes, mines are "H124SG * Ø 24 mm", they come in a box of 50 and should cost about 11 euros. 

That's all, if you like it or build it or need help feel free to write a comment, it will encourage me to write more articles :)

Saturday, 22 June 2013

Poor man's MSOP soldering

MSOP stands for Mini Small  Outline Package, that means that width of 4 leads is just 2mm, exactly the width of a row in a stripboard... Let see how can we solder that though.

On a piece of wood we tape carefully 4 thin wires. We start my taping one wire and then taping again a second one at a distance of 2mm, take as much time as you need, this step is crucial, place on top the MSOP to make sure they have the right separation. If you need to make any adjustments use the blade of a cutter to move the wire. Good now you have the top one and the lower one. For the 2 inner ones, just put them in between and fix them with tape, then use the cutter blade to make sure they are all spaced equally

We place the MSOP on top and we tape it, then make the necessary adjustments in the separations of the wires. Once we are happy we tape the IC this time more tightly.
 Time to solder, use flux and apply the iron firmly as with any other SMD
Once we have one side done we do the other side, this time it will be easier since the other half is firmly fixed, time to correct the separation of the wires too.
 Once we remove the tape we check the back of the IC to make sure all looks good and clean.
 We cut the thin wires by just applying some firm vertical pressure with a cutter.

 Time to  place it on the stripboard, we carefully separate the wires and we insert them through the holes,
 And voila, the breakout board :)