---

Tuesday, August 25, 2020

Testing the micro:bit's Analog Inputs

The BBC GPIO connection rings (labelled 0, 1 and 2) of a micro:bit can all be used as analog inputs.

You might think, that as long as you are careful to to exceed the 3V input voltage limit, then you can measure any low voltage whatever the source. Perhaps a photoresistor in a voltage divider arrangement with a fixed resistor. 

While this is basically true, if the source of the voltage to be measured has a high output impedance, at some point the voltage measured by the micro:bit will diverge from reality as the impedance of the voltage source being measured increases.

In reality, you can't measure something without altering it. The best we can do is to make the measurement errors small, so that they can be ignored.

This blog post determines the extent of this measurement error with the micro:bit's analog inputs.

Equipment Used

Here's a handy test program that reports the voltage at P0 when button A is pressed, that I used in this experiment.


Use a multimeter to measure the voltage between the 3V and GND ring connectors on the micro:bit and put this value in for Vanalog. I was powering the micro:bit from USB, so Vanalog was about 3.2V.

You will also need:

  • A digital multimeter on DC volts range
  • Resistors 100Ω, 1kΩ, 10kΩ, 100kΩ, 1MΩ, 10MΩ
  • A low-impedance voltage source such as bench power supply (set to 2V) or just an AA battery
  • An alligator lead

Setup

Here's the setup for the experiment.




The ground of the micro:bit is connected to the ground of the power supply and the positive lead from the power supply is clipped to one end of the test resistor. The alligator lead is used to connect the other end of the resistor the micro:bit P0 ring. The multimeter measures the voltage at the micro:bit's P0 ring (V1). V2 is the voltage reported on the micro:bit by the test program.

Results

Here are the results for a series of resistor values.

R (Ω)V1 (V)V2 (V)
01.992.05
1001.992.05
1,0001.992.05
10,0001.992.02
100,0001.991.77
1,000,0001.931.17
10,000,0001.750.92

Even the multimeter starts to give errors when connected to the voltage source via a 10MΩ resistor. The DMM used is specified as having an input impedance of 10MΩ but actually these results imply it's quite a bit better than that.

If we plot V1 and V2 against R, this is what we get:

As you can see, the micro:bit does pretty well up until a value of R of 10kΩ after which it goes down hill pretty rapidly.

Conclusion

If you want to use your micro:bit to measure a voltage, try and keep the output impedance of the voltage source lower than 10kΩ. 

Monday, July 13, 2020

Unit Testing and Arduino

When I worked in software, I was an early adopter of agile software development (eXtreme Programming) and so have always loved unit tests and TDD. Like many people from a software background, when I first started programming Arduino, my instinct was to write frameworks, patternize and objectify my code. Then I realised that I have 32k of program space to play with and a really big program might stretch to 100 lines of code (shock horror). So I adjusted my big-software thinking and like everyone else drew the line at functional decomposition and occasional library writing.

Background

For the fun of it, I recently decided to make a calculator (pocket calculator if you happen to have huge pockets) from the schematic design up.

For this project I wanted to use my new favourite low cost microcontroller (the ATTINY816) with Arduino IDE using Spence Konde's rather fantasic Arduino core.

To my surprise, making a calculator that actually does arithmetic well is a lot harder than expected. One obvious hurdle is that float in Arduino C is only 32 bits. So once your numbers get to about 7 or 8 digits they become disconcertingly approximated. So, I thought, I'm (or used to be) a computer scientist, I'll just implement my own floating point representation with more precision than you can shake a stick at. So, I did, as a C++ library (I'll put it on Github when I've got it working and tidied up).

However, for the first time, since I had first started using Arduino, I felt the need to write unit tests, to make sure that my number class was doing arithmetic and generating an 8 character string plus decimal point position information that I could then easily map onto an 8 digit 7-segment display.

Unit Tests

If you haven't used unit testing before, then the basic idea is that you write a load of test functions (the more the better) that exercise the code being tested in some way and compare the outcome with the expected outcome. So, for example in a number class, you might want to check that when you add 2 and 2 you get 4, but also you write a test to make sure that when you add -123.45 and 0.01 you get -123.44 and not -123.46.

Having a unit test suite behind your code means that if you change your code to fix one bug, when you run the tests it will immediately tell you if you have broken something else in doing so. As you build up your suite of tests, you get more and more test coverage for the tricky situations that may uncover bugs.

My Solution for Arduino

The solution I came up with is very specific to the problem, so, I'll try and include some general principals rather than just the code.

Firstly, I created a separate test sketch, specifically for the purpose of testing my number class, without any of the other code related to keyboard scanning and display refresh.

The second thing I did was to get out an Arduino Uno (well actually I used a MonkMakesDuino) because one of the great things about the Uno, is that compiling and uploading a sketch is MUCH quicker than the likes of an ESP32 or indeed the ATTiny816 programmed using UPDI. So, the round-trip time when adding tests or fixing code is greatly reduced.

I used the Serial Monitor to view the results of the unit tests, and a test pass would simply marked by a single line giving the name of the test that passed. A test failure, would include as much information as possible to help with debugging.



Here's the start of my test sketch:

#include "CalcNum.h"

char digits[] = "........";
const int numDigits = 8;

void setup() {
  Serial.begin(9600);
  testNums();
  testAdd1();
  testAdd2();
  testSub1();
  testMult1();
  testMult2();
  testMult3();
  testDiv1();
  testDiv2(); 
}

void loop() {}

My number class is CalcNum (imaginative right!).
digits[] is a data structure used by CalcNum in its writeDigits() method that prepares a string for easy mapping onto an 8 digit 7-segment display.

All the test functions to be called are then listed out in the setup function, as we only need to run them once.

The first of these (testNums()) tests the representation of numbers themselves rather than arithmetic, so lets skip on to the test function testAdd1():

void testAdd1() {
  CalcNum x = CalcNum(12, 0);
  CalcNum y = CalcNum(3, 0);
  CalcNum z;
  CalcNum::add(x, y, &z);
  test("testAdd1", z, "      15", 7);
}

This function defines two numbers (x and y) using an exponent form (x = 12 x 10^0 = 12).
Adds them together and then calls the general purpose function test to see if the result was as expected.

As an aside, I haven't used C++ operator overloading in my number class, as this would inevitably lead to the need for dynamic memory allocation, which I avoid like the plague when working on embedded systems.

So, what are the parameters to test?

The first is a string, that is the name of the test, the second is the CalcNum to be checked. the third is the expected result string from calling writeDigits() - in this case 15 with leading spaces. The final parameter is the expected position of the decimal point on the display (zero indexed, left to right).

Here's what the function test looks like:

void test(char *testName, CalcNum z, char *expected, int expectedDP) {
  z.writeDigits(numDigits, digits);
  int dp = z.dpPos(numDigits);
  if (strcmp(digits, expected) == 0 && dp == expectedDP) {
    pass(testName, expected, expectedDP, z);
  }
  else {
    fail(testName, expected, expectedDP, z);
  }
}

As you can see, the test function compares the calculated and expected 8 digit string and decimal point positions and if the match, calls pass and if they don't calls fail.

void pass(char *testName, char *expected, int expectedDP, CalcNum z) {
  Serial.print("PASS: "); Serial.println(testName);
  //report(expected, dp, expectedDP, z);
}

The function pass just prints out a message that the test passed, along with the name of the passing test. Note the commented out call to report. Sometimes this gets commented back in to shed light on why one test passed when another didn't.

The fail function is much the same as pass, but with a different starting message.

void fail(char *testName, char *expected, int expectedDP, CalcNum z) {
  Serial.print("**** FAIL: "); Serial.println(testName);
  report(expected, expectedDP, z);
}

The report function just prints out as much useful information about the result as possible, to help me fix the bug.

void report(char *expected, int expectedDP, CalcNum z) {
  Serial.print("got digits["); Serial.print(digits); Serial.print("]dp="); Serial.print(z.dpPos(numDigits));
  Serial.print("\t expected ["); 
  Serial.print(expected);Serial.print("]dp="); Serial.println(expectedDP);
  Serial.print("float: "); Serial.println(z.toFloat(), 10);
  Serial.print("m: "); Serial.print(z.m); Serial.print(" e:"); Serial.println(z.e);
  
  Serial.println();
}

Conclusion

Once this project is written, I probably won't write any more tests until I meet a similar project for which 'it seems to work ok' is not sufficient.

However, its really easy just to put together some tests if you need to. For me, it wan't even worth looking to see if anyone had made a framework to do this and then taking the trouble to work out how to use it.

I hope this write-up will help you if you find yourself needing some Arduino unit tests!





Sunday, April 12, 2020

Making Protective Face Shields with a K40 Laser Cutter - Part 3

Part 1 Part 2

We finally had a chance for the household to go ahead and make some shields.



Cleanliness

This was tricky, I don't have gloves, but do have a face mask each that covers our mouths and noses. So, I would not count these masks as sterile and as such have been careful to pass on that information to recipients.

So we had frequent hand washes and (for what its worth) baby wipes to wipe down the plastic that gets a bit of a dirty residue from the smoke produced during cutting. 

Fortunately the masks are easy to clean.

Finding Recipients

While waiting for the laser cutter to do its thing (about 8 mins / shield) there was plenty of time to open up Google Maps and search on care homes nearby. I emailed (where there was an email address on the website) or otherwise phoned a total of 5 homes within walking distance. 
One asked for 12 on the phone and was very grateful as they had no face shields at all. Another said the same on email (yesterdays delivery). And already we have two more deliveries to make tomorrow to care homes and GP surgeries. There is clearly a huge demand here in the UK.

What Next?

The next step is to find a cheaper source of material than eBay! and find out if there is a better way to distribute.

Although it seems that hospitals are the main focus and care homes and GP surgeries have rather been left to fend for themselves. So I feel that providing them with masks, fits both my small scale of production and a need that if it reduces infections will lighten the hospital load at the front end.

Tuesday, April 7, 2020

Making Protective Face Shields with a K40 Laser Cutter - Part 2

In part 1 of this post I looked at laser-cutting the plastic band. In this post, we can now attach the clear A4 sheet to the shield.

I used this from eBay with a thickness of 140 micros for the visor, which seems about right.


The A4 clear 'acetate' sheet fits in landscape aspect ratio and is punched with 4 holes. I used one of these:

.. and put one pair of holes in the middle, as you would a normal sheet of paper for a ring-binder. I then carefully put the other holes in at the edges by lining up one side of the hole punch puncher with an existing hole. This is fiddly, so 
Edit: This works fine,  didn't realize that if you push the guide rail on the side most of the way until it says A5 (on ours) punch one set of holes then flip the acetate sheet through 180 degrees and punch again, the holes are in the right places. Try this on paper first to avoid wasting acetate.


I have ordered one of these: which should make all the holes in one go.

The little plastic tabs on the strap fit snugly into the holes and hold the 'acetate' sheet in place.


Review

The inner band holding the head away from the visor works well and prevents misting up. The whole thing was comfortable even wearing glasses. I'm sure its a way from rigorous professional protection equipment, but I think its a lot better than nothing, or just a face mask on its own.

It's also entirely plastic and I would have thought washable. I have three of these now, which we are going to send to medical and carer friends of ours who have expressed an interest.

Now I know its possible on A4, I'm ordering more materials and we'll get cracking. I'll come back with a part 3 on manufacturing cleanly and experiences in donating these.

Other Resources

This all stems from the work here https://community.andmirrors.co.uk/t/covid-19-laser-cut-face-shield/168 and https://www.kitronik.co.uk/blog/kitronik-make-nhs-frontline-ppe-visors/

There are lots of other projects and designs out there and Google will stay a lot more up to date than any list I put here.

Monday, April 6, 2020

Making Protective Face Shields with a K40 Laser Cutter - Part 1

This blog documents my efforts to make protective face masks using a low cost K40 laser cutter using Kitroniks visor design.

The A4 'acetate' sheets for the transparent shielding bit haven't arrived yet, so consider this post a Part 1 , dealing just with making the strap shown below. The clear visor part will come later.



Early indications: Yes, it's feasible - if you have a K40, give it a go.

K40

These laser cutters are very low cost, but only have a small bed (about 330x220mm useable).


Their bed is much too small to take the files that Kitronik designed for their cutters, but if you have a bigger laser cutter then don't mess around with A4 it will be much more efficient to go to the full-size design here.

Design Files

Download the design files for A4 from github here or direct zip download here. The original DXF file was created by Dave at Kitronik and kindly passed on to me by Loraine Underwood, a fellow K40 owner (hers is in bit ATM)

The file A4 face shield band.dxf is the original file from Dave. I use the open source K40 Whisperer to control my K40, and this expects files to be an SVG, with vector cut lines to be in red 0.1mm wide. So the file A4 face shield band k40 whisperer.svg is an SVG version of the original with the lines made suitable for K40 Whisperer. I have also spaced things out a bit, because the K40 can be a bit 'melty' and close together lines can cause problems.

If you make a version of this for the default and highly suspect original K40 software, then please let me know and I'll add it to the repository.

The really nice thing about this design is that it just uses two materials, 0.5mm Polypropylene for the strap material and standard clear A4 OHP acetates (used full size without the need to cut). Whats more everything just clips together, no need for glue or staples.

Material

I bought a few sheets of Polypropylene Plastic Sheets 0.5mm here. It seems to be quite widely available as A4 to try. I now need to wait for a load more. Kitronic also sell this material, but not as A4. However buying some big sheets and cutting them up may work out cheaper.

Eventually you will need the clear 'acetate' sheets to use as a visor - Kitronic recommend this. My sheets haven't arrived yet, they will be discussed in part 2 of this blog.

Cutting Parameters

Your parameters will vary so, please cut a small test piece before wasting material. This was what I found worked best for my K40. Speed for vector cut 15mm, power (set by the knob on the k40) about 50%. My beam seems to diverge at higher powers than that, but I probably don't have the focus right.

The material is quite light, and I have seen people use small magnets in this situation to keep the parts flat to the bed. I didn't use magnets and it worked ok for me. But if yours starts flapping about, you know what to do.

Assembly

Please note that what I am describing here is making the first visor so, I am paying no attention to cleanliness at all. When making these for real, I think it would be sensible to be gloved and masked.

It took me a while to figure out how the pieces fit together, so here's what I learned. I have put numbers on each piece to make it easier to reference the parts.

1. Here's the cut sheet.


There is a bit of singing and in places the plastic had stuck itself together again. But I found it separated from the sheet easily enough.

2. The Pieces Labelled

For your first make, I'd suggest marking each piece like this (a sharpie will do). Or you may just be a lot better at puzzles than me and consider all this unnecessary!

3. Join Pieces 1 and 2
Bend the tabs of piece 1 and push them through the horizontal slots in part 2.

4. Join Pieces 2 and 3
Piece 3 feeds through the two end vertical slots in piece 2 like a belt through a buckle.

5. Join Pieces 1 and 4
Piece 4 fits on to piece 1 in much the same way.

6. Outer Strap
You should now have a long strap.

7. Inner strap
The inner strap is required to keep the clear part away from the wearer's face. Piece 5 accomplishes this by making a bow in the main strap.

Start by joining piece 5 to piece 2 and then to piece 1 using the horizontal slots.

8. Final part
The basic shape is there now, so it just remains to attach the buckle that connects the two ends of the strap together.
Join 4 and 6 together first. These have two positions, presumably for big and small heads. Fine adjustment of size uses piece 3.

My Donation Plans

Making these on a K40 is slow and so I don't expect to be making more than a few hundred of these visors at most. So, personally, we will be donating these to care-workers and other people we know who are desperate for protective wear and not too worried about the quality on the basis that something is better than nothing. I think, I'll leave supplying to the NHS as something for the big players (with big laser cutters) to do. But that's just what I think.



I hope to post part 2 of this tomorrow, if the clear plastic arrives.

Good luck and let me know how it goes.

Tuesday, February 11, 2020

Installing OpenCV 4 on a Raspberry Pi 4

I have recently been updating the Computer Vision chapter of the third edition of my Raspberry Pi Cookbook. Way back when I was writing the seconds edition, I decided to use the SimpleCV wrapper to OpenCV because it was -- well very simple to use.

SimpleCV is no longer maintained and in any case requires Python2. So the chapter's code needed to be changed to use OpenCV directly. Here's what I had to do on a Raspberry Pi 4 running Raspbian Buster (10).

To install OpenCV, first install the prerequisite packages using these commands:
$ sudo apt-get update
$ sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103 
$ sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5
You may also need to update pip using:
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py
Then install OpenCV itself and Python image utilities using these commands:
$ sudo pip install opencv-contrib-python==4.1.0.25
$ pip install imutils
After installation is complete, you can check that everything is working by starting Python 3, importing cv2 and checking the version:
pi@raspberrypi:~ $ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.0'
>>> exit()

Friday, November 8, 2019

Making an SD card out of 1960s transistors

Reading about flash memory, I was staggered by the astronomical number of transistors in such a small place I decided to see what it would take to make one of these using 1960s technology.

     

Flash Memory: Lets go with a modest 32GB micro-SD card.

Transistor: I'll use a TO-18 package transistor. If like me you are from the UK and of a certain age, then the BC108 or BC109 will bring fond memories of Wireless World magazine and minor soldering injuries. In the US, I think the 2N2222 was more popular.

How many transistors are there in a 32GB SD Card?

Each bit of flash memory is made up of a single special type of transistor (called a floating gate transistor) that uses the quantum tunnelling effect. I choose to understand this to mean that you can write to the memory bit electrons get trapped in an insulated region and you can tell they moved (and so whether they are a 1 or a 0) by how much current flowed. That's my probably erroneous and over-simplified understanding, but hey, it works for me.

So, bipolar junction transistors such as the BC109 and 2N2222 have been around since the 1960s and can in fact still be bought today. These transistors are not capable of storing a bit of data on their own, to do that, you would need to arrange two as a flip-flop. But remember this is a thought experiment, as you've probably guessed, it isn't actually possible to build a 16GB flash memory from 1960s transistors.

And so, to the calculation:
32 GB means roughly (yes I know a GB is often taken to be 2^30) 32,000,000,000 bytes of data.
Each byte comprises 8 bits, so let's say there are 8 x 32,000,000,000 =  256,000,000,000 bits of data.

Not only that, but the card will contain a whole load of transistors for control logic and caching. For the sake of this thought experiment, I am going to ignore this. So the answer is there are something like:
256,000,000,000 transistors in a 32GB flash memory

Making this with TO-18 Transistors

Lets pretend we can make this thing with our TO-18 package transistors arranged in a square grid. To make a grid of 256,000,000,000,000 transistors is going to require a grid with the square root of 256,000,000,000,000 transistors on each side.

Thats roughly:

506,000 transistors per side





Each of our 1960s transistors is in a metal can with a diameter of about 5mm. If we assume that we need a bit of space around each transistor, then let's space them out to one transistor every 8mm. This means that each side of our array of transistors will have a size of 8mm x 506,000 or 4,048,000mm which is 4,048m which is roughly 4km (or roughly 2.5 miles)

A square 4km or 2.5 miles on each side


Let's superimpose a square 4km on each side on Manhatten.



Each of our little transistors weights about 0.3g so 256,000,000,000 would weigh:

76,800 metric tonnes 

- and thats ignoring the much greater weight of what it was mounted on.

When it comes to power consumption it's pretty hard to make anything useful up, especially as the distances involved mean that it would have no chance of actually working, even if our transistors were suitable. But let's just assume it would need its own power plant.