Thursday, June 14, 2018

Cuban Makers at "Copincha" in Havana

Last week I found myself in front of a room full of Cubans in Havana, delivering a course on Arduino in broken Spanish. Was this a variation on the kind of dream where you are sitting down to an exam without having done a stroke of revision? Or the dream where you find yourself at work having forgotten to put on your trousers? 
The sequence of events that led to this radical departure from my comfort zone - not so much stepping outside it, but more waving it goodbye and jumping on a plane - started a couple of years ago when I was contacted by Jonnet who had seen a Spanish language edition of one of my books and wanted to borrow it for her Cuban partner Mauri.
Mauri and Jonnet were keen to establish some kind of makerspace in Havana to provide a place for local makers could come and share ideas, tools and resources. Cubans, by necessity, are an inventive and creative group of people. Resources are scarce and so a make-do-and-mend culture is totally natural to them. If ever there was a race of makers, it is probably the Cubans.
My wife Linda and I have been learning Spanish for years despite little evidence of success. We also spent our honeymoon in Cuba and have very fond memories of the country. And so, to cut a long story short, in a rash moment I volunteered to run an Arduino course at their makerspace when it was up and running. And a year or so later, that is how I found myself in front of a group of Cubans, talking about Arduino in Spanish.
Mauri, the driving force behind the makerspace also lives in the house containing the makerspace in quarters above the two rooms that make up the space.  The space is called "Copincha" - "pincha" is Cuban slang for "work" and so "copincha" is a way of saying "co-work".
Mauri and friends have used scrap wooden packaging cases to make box stools for sitting on as well as large work tables with table-tops that are removable to reveal ample storage space for tools and supplies to make best use of the space. There's probably ten square meters of table top and maybe twenty stools for sitting on. That is quite some labour of love!

My Arduino course was the first course to be run at the makerspace and since we had mixed levels of ability, with some very skilled Arduinoistas, I ran introductory Arduino lessons in the morning and in the afternoon offered "consultations" to the advanced groups who had projects that they wanted to work on.
I brought Arduino compatibles (actually MonkMakesDuinos) together with basic component kits and breadboards with me but the course relied on the students having access to a laptop to program the MonkMakesDuinos. In true Cuban style, this involved a fair amount of borrowing and inventive solutions. For example, one student used a homemade Raspberry Pi laptop to program the Arduino. Another used a Windows tablets with a pop-up keyboard. With sharing and cooperation everyone was able to participate.
I am very pleased to hear that the course is due to be repeated in July (when many Cubans are on leave from their day jobs). This time, the course is being delivered by Yusinier, one of the local experts at the course who amongst his many skills in art and technology has taught Arduino to university students.
The course and supporting code is available as open source on GitHub here and I would very much welcome any pull-requests to improve the Spanish.
Here are just a couple of the things that the course attendees made during the course:
Juan, a retired mechanical engineer with a passion for replica cars, worked on adding automation to a fibre-glass modelled replica he was building from scratch.

The chassis of the car was built to use a single gear-motor as rear-wheel drive and a servo motor to control the steering. This was combined with an ultrasonic range-finder to detect obstacles and invoke an avoidance function that involved a randomly chosen turn to the left or the right, followed by reversing. The design also included automatic brake lights. The steering mechanism was made entirely from waste pieces of plastic and skilful use of a Dremmel.
Juan has an enormous collection of vintage model cars that he restores, some of which are for display and some for sale at a shop he runs with friends, at San Lázaro 1060 entre San Francisco y Espada - somewhere around here.
Yusinier is a visual artist and film maker and amongst other things has to devise his own props, such as remotely triggered fake bullet strikes. He often uses Arduino and his project during the course was the controller for a motorized camera dolly.

In England, when I need components, an Arduino or a cheap module from China, all of these things are easily obtainable with a few clicks of a mouse. This is not so easy in Cuba. For most Cubans a £30 Raspberry Pi represents more than a months pay. Even if they want one, they cannot just order it from Amazon or any of the other sources that we take for granted. Typically, getting such things into Cuba relies on visitors to Cuba like us bringing things with us when we visit. Donating things as personal gifts to Cubans. Visitors to Cuba are advised in the guide books to bring crayons, bars of soap and Lego as gifts so why not things for makers?

Perhaps if you are planning a trip to this beautiful country and will be visiting Havana, you could contact Mauri (maurice.haedo@gmail.com) and see if he could make use of a few Arduino clones, or eBay specials for "Copincha". If you do, you will be assured of a warm welcome a great coffee and a glimpse behind the scenes of  this amazing country.

Friday, March 9, 2018

Review and Test of the MH-Z14A NDIR CO2 Sensor

The MH-Z14A measures CO2 concentrations using absorption of infra-red light. These devices are available on eBay, Amazon and other places from about $20.

Ignore the capacitor across the power pins - this was a failed attempt by me to improve the reliability of the analog outputs.

My initial impressions of the module were pretty favourable. It looks pretty space-age with its gold coated chamber and 4 LEDs in the corner of the fabric window that allows the air to enter. These LEDs blink once a second as the readings are taken.

You can find a datasheet for the module here.

The device is available configured for different ranges of CO2 concentration. This module uses the most common range of 0 to 5000ppm.


The module actually has three interfaces - described in the datasheet.

  • Analog output - proportional to the ppm of CO2 range of 0.4 to 2V for full range.
  • PWM output - very slow PWM over the period of a second, with the HIGH duration being proportional to the ppm of CO2.
  • 9600 baud 3V/5V TTL UART interface. You send a message saying you want a reading, it responds with a reading.


The module has lots of pins on it, most of which are duplicates. I just stuck to the main 0.1inch pitch 2 x 6 connector on the right. Note that the pin numbering on the board reflects the evolution of various connectors added through versions of the boards, and is therefore a bit odd.

First Use

My first experiment was just to power-up the board (5V to pin1, GND to pin 2) with a multimeter on the analog output (pin 4).

As soon as it powered up it was obvious that every second or so, the LEDs behind the fabric would light up and the current would surge to around 100mA. This was very short in duration so I expect the average power consumption of this unit is actually pretty low.

The analog output on pin 4 indicated 0.85V using the datasheet the ppm is calculated as:

ppm = (Vout - 0.4) * (5000 / 1.6)

In this case a ppm of 1406. This was wrong, I was in a big room with the door open and not breathing on the sensor, so I would expect a reading of around 400ppm.

It looked like the device was not calibrated. Or, calibrated in a somewhat unhealthy factory.


You can calibrate by sending a serial message (see later) or by connecting pin 8 to GND for at least 7 seconds. Using the pin 8 methods, the new voltage reading was a much more believable 0.52V indicating a plausible 375 ppm.

After huffing on the sensor for a bit, it was easy to get the readings to go up and then work their way back down to ambient levels after about 5 minutes.

Its looking good!


The next step was to hook the device up to an Arduino Uno so that I could compare the reading methods and also do a bit of logging.

Here is the test script that displays the readings from all three output methods in the Arduino Serial Monitor once every 10 seconds.

#include <SoftwareSerial.h>

const int analogPin = A0;
const int pwmPin = 9;

const long samplePeriod = 10000L;

SoftwareSerial sensor(10, 11); // RX, TX

const byte requestReading[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
byte result[9];
long lastSampleTime = 0;

void setup() {
  pinMode(pwmPin, INPUT_PULLUP);

void loop() {
  long now = millis();
  if (now > lastSampleTime + samplePeriod) {
    lastSampleTime = now;
    int ppmV = readPPMV();
    int ppmS = readPPMSerial();
    int ppmPWM = readPPMPWM();

int readPPMV() {
  float v = analogRead(analogPin) * 5.0 / 1023.0;
  int ppm = int((v - 0.4) * 3125.0);
  return ppm;

int readPPMSerial() {
  for (int i = 0; i < 9; i++) {
  //Serial.println("sent request");
  while (sensor.available() < 9) {}; // wait for response
  for (int i = 0; i < 9; i++) {
    result[i] = sensor.read(); 
  int high = result[2];
  int low = result[3];
    //Serial.print(high); Serial.print(" ");Serial.println(low);
  return high * 256 + low;

int readPPMPWM() {
  while (digitalRead(pwmPin) == LOW) {}; // wait for pulse to go high
  long t0 = millis();
  while (digitalRead(pwmPin) == HIGH) {}; // wait for pulse to go low
  long t1 = millis();
  while (digitalRead(pwmPin) == LOW) {}; // wait for pulse to go high again
  long t2 = millis();
  long th = t1-t0;
  long tl = t2-t1;
  long ppm = 5000L * (th - 2) / (th + tl - 4);
  while (digitalRead(pwmPin) == HIGH) {}; // wait for pulse to go low
  delay(10); // allow output to settle.
  return int(ppm);

Here is a sample of the results from the Arduino Serial Monitor.

The first column is the analog reading, the middle column from the PWM interface and the right column from the serial interface.

The analog readings are all over the place. I don't think this interface is useable.

UPDATE: The analog output actually appears to be unbuffered. Using my DMM which has a Zin of about 10MΩ the voltage readings were within a couple of % of the serial readings. It was the relatively low Zin of the Arduino ADCs sample and hold messing things up. So, a unity gain op-amp buffer should do the trick.

The PWM interface is pretty consistent with the UART interface, although every so often there will be a bad reading.

The Serial interface is clearly the winner and if you implement the checksum algorithm specified in the datasheet I would expect 100% reliable readings.


To give the module a long term test, I set it running overnight in out bedroom with the following somewhat worrying results.

The y-axis is ppm of CO2 and the x-axis time (1 sample per minute).

I used the PWM measurement method and you can see the frequent misreadings (perhaps 1 in 50). But the trend is pretty good, showing the ppm rising well above 4000ppm, before the bedroom door was opened in the morning and the readings fell back to ambient values of 400ppm or so. Yes, we were all still alive!

The high overnight readings seem worryingly high for me, but at the moment, I do not have a calibrated sensor to compare with.


The sensor module's Serial interface works well and the PWM interface acceptably but the analog interface is only useful if buffered.

A serial interface that it both 5V and 3V compatible makes the module suitable for use with a Raspberry Pi or Arduino without the need for level conversion.

I cannot comment on the accuracy of the sensor without comparison to a trusted device. When I get one, I will come back here for an update.

Tuesday, December 5, 2017

Bluetooth Control of a micro:bit and motor using Bitty

Bitty is a suite of software that allows you to use your smartphone as an interface to your micro:bit. The software comes in two parts. A resident program that you download from here as a HEX file and copy onto your micro:bit and an App that you install on your smartphone (search your app store for Bitty Blue.

Here's a video of me using Bitty Blue to turn a MonkMakes Relay for micro:bit on and off and hence control a motor.

The Bitty Blue app has a load of other fun things to do with your micro:bit, so do try them out as well.

Bitty also have a range of other apps for things such as:

  • Turning your micro:bit into a wireless data logger (for which a Sensor for micro:bit might be handy)
  • Bluetooth control of a Kitroniks robot.
  • Controlling lighting
  • An audio prank
  • even a Christmas tree bauble

Monday, October 16, 2017

How to Get Started in Electronics

I sometimes get asked how to get started in electronics and so at Maker Faire New York 2017 I gave a short talk on this topic. In particular I looked at how hobby electronics now almost inevitably starts with the use of a microcontroller board like the micro:bit or an Arduino or a single board computer like the Raspberry Pi. The question then arises, which do you pick?

Here are the slides from this presentation. You can also find a video of it here (although there was a lot of noise).