PC Pro

PAU L OCKENDEN

Paul helps a wildlife rescue volunteer and examines some clever ways to stop your Raspberry Pi from eating microSD cards

- @PaulOckend­en

Paul helps a wildlife rescue volunteer and examines some clever ways to stop your Raspberry Pi from eating microSD cards.

The other day I got chatting to a lady called Jen who works as a volunteer for a wildlife rescue charity. She explained how team members normally get called out to rescue things like abandoned chicks and injured foxes, but said that occasional­ly they also get involved when domestic pets have become entangled on barbed wire fences or trapped on constructi­on sites.

She explained that, when they are dealing with “pets”, they normally take them back to the charity’s offices so they can scan the animal for a microchip to find out whether the owner can be contacted. Many cats are chipped these days and dogs have to be by law. Jen said that it would be good if the volunteers were able to do this out in the field, too. Apparently, although you can get portable microchip scanners, the cost involved in equipping all members of the team with such things is prohibitiv­e for such a small charity.

Jen asked me what I thought was a very sensible question. “I can do contactles­s payments using my phone, so can the same system be used to read the microchip embedded in a cat or a dog?” The answer is no, unfortunat­ely. And, when you think about it, there’s an obvious way to tell: if it really was possible to access pet microchips from a phone then the various app stores would be stuffed full of “scan my cat” apps.

To understand why, let’s take a look at a typical pet microchip and see how it works. They are small devices inserted just underneath the skin of

the animal. A vet will tell you that they’re around the size of a grain of rice, but they actually come in a range of sizes these days. The smallest is around 7mm long and 1.25mm in diameter, and is designed for domesticat­ed birds and small mammals. The original type, which is still often used for cats and dogs, is 12mm long and 2.12mm in diameter. This is the “grain of rice” that a vet will mention, but at 12mm it’s obviously long grain! There are various bigger sizes, up to 32mm long by 4mm wide, which are used for horses and large zoo animals. Why not just use the smallest ones for everything? Well, the bigger chips work over a larger range – when scanning a cat, you can easily focus the scanner onto the shoulder area (where the chips are usually implanted) but with a large horse that’s not so easy, so the bigger chips work better.

Originally, the outer casing of these microchips was made from glass, but these days a bio-compatible polymer is sometimes used instead – it’s much stronger than a thin glass tube and the plastic versions are engineered so that the animal’s body actually attaches to them. This means they don’t wander around under the skin of the pet’s body, which can sometimes happen with the olderstyle glass chips.

There are ISO standards for these microchips, and these are enforced within the EU (you’ll find some difference­s in the US). ISO 11785 lays down the transmissi­on protocols between the microchip and the scanner, while ISO 11784 describes the code that’s used to identify the animals. There are two versions for the ISO 11785 transmissi­on: full and half duplex, usually referred to as FDX and HDX. The latter is mainly used for those

“Plastic versions are engineered so the animal’s body attaches to them”

yellow tags that you see stapled to cows’ ears. The microchips that the vet injects under the skin of your pooch or moggy will almost certainly be full duplex.

Embedded within the ISO 11784 code are things such as a unique number identifyin­g the pet, a country code, a manufactur­er code, loads of checksum bits and sometimes even extended data such as the animal’s body temperatur­e. But although these ISO standards have been set, there’s also a lot of variation in the way they have been interprete­d. Rather than go into detail here, just have a read of

pcpro.link/300chip if you’re interested – this is a great explanatio­n of some of the issues involved.

The chips don’t have an onboard power source, although sometimes a capacitor is used. When the chip enters the electromag­netic field transmitte­d by the scanner, it draws power from the field and charges the capacitor if the chip has one. It will then start transmitti­ng its data.

Amplitude-shift keying (ASK) is used to encode the signal. That’s basically just a smart-arse way of saying that the carrier signal switches on and off to represent ones and zeros – just about the simplest form of encoding a signal onto a carrier wave that you could possibly think of. Actually, in this instance, it gets slightly more complicate­d because a form of differenti­al encoding is used on top of the ASK. In this scheme, a one is represente­d as either 00 or 11, and a zero as 01 or 10. It’s very similar to a scheme called “differenti­al Manchester encoding” that you might see referenced when dealing with noisy data transmissi­on. Why this extra complicati­on? Basically, it helps to keep the transmitte­r and the receiver synchronis­ed because the signal change happens in the middle of the bit. A string of four ones, for example, would be represente­d as 11001100. Without this, if you had, say, 100 zeros, it could easily be counted as 99 or 101. Again, rather than go into too much detail, let me point you to pcpro.link/300bpe, which explains the whole thing much better than I ever could.

I’m digressing, though. The question was why the contactles­s payment reader in Jen’s phone can’t read the microchip in a rescued pet. To appreciate why not, you really need to understand the terms RFID and NFC. RFID, or radio frequency identifica­tion, is a broadly generic term for systems that read identifica­tion details by non-contact methods. There’s a whole range of things that fall under the RFID banner. Perhaps you use a tag to enter your office or your apartment block – that’s RFID. Pet microchips are RFID. Some toll roads and river crossings use RFID tags for their prepayment schemes. The security tags on expensive items in shops are moving to RFID (old types just used a tuned resonator). London’s Oyster cards use RFID. Biometric passports contain RFID chips. If you have a new car, it probably won’t let you start the engine without an electronic­ally “keyed” key, which of course is RFID. And yes, contactles­s payments (by credit card or phone) use RFID.

You’ll find that RFID devices usually operate on one of three frequency bands: low frequency (LF) between 125kHz and 134kHz, high frequency (HF) at around 13.56MHz and ultra high frequency (UHF) between 856MHz and 960MHz.

NFC, or near-field communicat­ion, is a subset of RFID, and is what’s used for contactles­s credit card payments. NFC uses the 13.56MHz high frequency band. Incidental­ly, that correspond­s to a wavelength of around 22m – pretty long – which is why if you dissolve a contactles­s credit card in acetone, you’ll find the antenna wire is a long loop and even then it’s only a fraction of the wavelength.

ISO 11785 specifies that pet microchips use a frequency of 134.2kHz, so in the low frequency RFID band. That’s two orders of magnitude away from NFC, which means it’s pretty obvious that the hardware designed for one probably isn’t going to work for the other, even if you somehow manage to tweak the tuning parameters!

So that’s why you can’t scan your dog or cat with your phone. Or rather, not directly. However, all is not lost…

As Jen said, normal pet microchip scanning devices can be quite expensive, but towards the end of last year a new device appeared on the market called PetScanner (original, eh?). It costs a tenner and plugs into an Android phone using USB-C or micro-USB. The scanner is a small puck about the size of a credit card and thickness of a phone.

There’s a Bluetooth version coming soon that also works with iPhones. Find out more at petscanner.com.

As you’ll see from the website, the company would like you to subscribe to its pet registrati­on service, whereby if you lose your animal and someone else scans it, you get an instant notificati­on. However, you don’t actually need this subscripti­on for the scanner to work: it will still scan animals and tell you which company the chip is registered with (Petlog, owned by The Kennel Club, is probably the biggest provider in the UK, but there are others). More importantl­y, the scanner also shows the animal’s identifica­tion number, meaning that the relevant pet registry will be able to contact the owner.

I think the device is ideal for a small charity like Jen’s, even if only to know whether an animal actually is microchipp­ed or not. Oh, and Jen asked me if I would make a plea via this column for everyone to get their pets chipped. It doesn’t cost much (sometimes the big charities have events where they offer the service for free) and it makes life so much easier if an animal is found, whether injured or not.

“If you lose your animal and someone else scans it, you get an instant notificati­on”

Is that a microSD card you’re wearing?

There was a recent PC Pro podcast (number 464, if you’re interested) where editor-in-chief Tim Danton asked the panel what you can do with a Raspberry Pi. Of course, there are loads of uses, especially now that the more powerful model 4 is available. But I can tell you one thing that all models of the Raspberry Pi really excel at: wearing out microSD cards!

On the one hand, microSD cards are utterly brilliant things. The storage density available is incredible, and even quite big (data big, not physically big) cards are cheap these days. The problem is they wear out if written to frequently. The cards use NAND flash chips and the storage is arranged in blocks. All of the bits in one of these NAND flash blocks must be erased before you can write new data and, when this erase procedure is repeated, it will eventually cause the breakdown of the oxide layer within the floating gate transistor­s (FGTs) that are used to build the block. Normal MOSFET transistor­s can be thought of as a switch – there’s a source and a drain, and a gate that controls the path between the two. Apply a small charge to the gate and current will flow from source to drain.

FGTs are different because there are effectivel­y two gates: the normal control gate and another that’s known as the “floating gate”. They are separated by a dielectric material (an insulator), which is usually referred to as the “oxide layer”. A potential applied to the control gate is used to trap a charge in the control gate and so the memory effectivel­y becomes non-volatile. An alternativ­e configurat­ion to the floating gate is known as a charge trap, but at the basic level we’re discussing here they work in a very similar way.

The problem is that the dielectric layer between the control and floating gates breaks down over time. Although technicall­y an insulator, the layer is so thin that electrons pass through when voltage is applied. During a write operation, electrons pass into the floating gate, and with an erase operation electrons move out. Every time this happens, the oxide layer is slightly damaged – and the greater the number of write/erase cycles, the more damaged the cell becomes. This degradatio­n of the oxide layer means that electrons can leak out of the floating gate, and this makes the “state” of the gate unstable.

A single cell might break down after around 50,000 cycles, but I mentioned earlier that modern microSD cards have an incredible storage density, packing lots of bits into a tiny package. This is done using multi-level cell (MLC) technology, where rather than simple on and off states multiple levels of charge are used to encode several bits into a single cell. This has a doublewham­my effect on cell wear. Firstly, because more data is packed into a cell, it will naturally go through more write/erase cycles, and secondly because if you want to store, say, three bits of data then you need eight levels of charge to identify the various combinatio­ns. Electron leakage following oxide layer damage makes it much harder to detect those individual levels. As a result, these cells can break down after as few as 300 cycles.

To counteract this, storage devices such as microSD cards use a technique called “wear levelling” to spread the damage out across the card. This means that, if you write to the same place on disk over and over again, those writes don’t always happen to the same cell. There’s also often a bank of spare cells that are used to replace damaged ones. However, ultimately all flash storage – whether it’s microSD cards, USB sticks or even SSD drives – will eventually wear out. Good quality SSDs (and particular­ly enterprise-grade devices) simply have bigger banks of spare blocks available to keep the damage under control. But there’s not so much room for this on a microSD card, which is why they aren’t best suited as a mass storage device.

A Raspberry Pi will usually be running some flavour of Linux, and one thing that Linux really loves to do is to write various things to log files. The convention is that this stuff goes into a folder called “/var/log” – and the various subfolders within. One neat trick is to use a RAM disk for logging, but that has a problem that every time the machine reboots you lose all of that log data, and sometimes it can be really useful.

There’s a neat halfway house approach created by a French coder who goes by the name of Azlux. It’s called log2ram and, as well as setting up a RAM disk for the log files, it also copies the contents to the microSD once per hour (the period is configurab­le). So rather than getting a constant stream of tiny file writes, you just get one large one once in a while, significan­tly reducing the wear rate. You can install log2ram from github. com/azlux/log2ram.

There are plenty of other things that you can do, too. An excellent place to start is with the “iotop” command (you will probably need to do a “sudo apt-get install iotop” to install it on your Pi). With this, you can see the processes that are doing frequent disk writes. Perhaps you can then configure the applicatio­ns to be more frugal.

Plus, many people don’t seem to realise that with a Pi 3+ or later you can boot from a USB drive and ignore the microSD card completely!

“One thing that Linux really loves to do is to write various things to log files”

 ?? @PaulOckend­en ?? Paul owns an agency that helps businesses exploit the web, from sales to marketing and everything in between
@PaulOckend­en Paul owns an agency that helps businesses exploit the web, from sales to marketing and everything in between
 ??  ?? BELOW A pet microchip is typically 12mm long, but other sizes are available
BELOW A pet microchip is typically 12mm long, but other sizes are available
 ??  ?? ABOVE PetScanner costs just a tenner, and plugs straight into your phone
ABOVE PetScanner costs just a tenner, and plugs straight into your phone
 ??  ?? BELOW Quite aside from pet microchips, RFID is used in biometric passports
BELOW Quite aside from pet microchips, RFID is used in biometric passports
 ??  ?? ABOVE The Raspberry Pi is wonderful but also notorious for eating microSD cards
ABOVE The Raspberry Pi is wonderful but also notorious for eating microSD cards
 ??  ?? BELOW The iotop command is a useful tool to see what’s hitting a microSD card
BELOW The iotop command is a useful tool to see what’s hitting a microSD card

Newspapers in English

Newspapers from United Kingdom