Monday, 1 June 2026

ABS Sensor Testing

My son's car (2016 Peugeot 208) quite suddenly threw up some alarming-looking warnings recently about ABS, ESP and tyre-pressure monitoring not working. Reading the error-codes from the ECU memory threw up DTC C1331, indicating a fault with the right-hand side ABS sensor:-

 
For reasons that needn't detain us here, I wound out swapping out both front sensors - presumtively one good and one bad.  
 
What is someone with an oscilloscope and some time on their hands supposed to do?
 
I had already determined that the sensor was a hall-effect sensor that couldn't be tested with a multimeter. It was an "active" sensor that required power to operate.  The sensor part number (for the benefit of future generations searching for this information) is PA66-GF26.  It has a two-pin connector on it.
 
 
I struggled to find any good information about how this sensor should be connected up and what the output of it should look like (and indeed how the output of this active sensor would be communicated to the ABS Control Unit, considering that it needs power and only has two pins.  It looks like pin 1 takes power via a resistor (that is internal to the ABS module) and pin 2 is ground:-
 
 
 
The meter in the diagram shows where I hooked up a multimeter (initially) and an oscilloscope (later).  
 
The outcome is interesting.  I had assumed I would see a simple binary "on" or "off" depending on the presence of a magnetic field. That's not what I got at all. Instead, the sensor sends an 18KHz square wave (varying from about +2V to +8.5V) on the + line when there is a magnetic field present.  When there is no magnetic field present, the line just sits at +5V.  There are also periodic (every 2ms) "gaps" in the 18KHz signal, presumably so the ECU can distinguish between a real signal and just any old 18KHz noise.
 
Here are the oscilloscope traces captured with a magnet held close to the sensor:-
 

 The horizontal and vertical cursors show what's going on.  The vertical cursors show the signal on pin 1 being varied from +2.1V to +8.4V (which can only happen is there is a fairly high output impedence on the input to pin 1: I took a complete shot in the dark and used a 1K resistor between the 12V supply and that pin).  The horizontal cursors show the 2ms interval between "gaps" in the 18KHz signal, presumably to allow the ABS module to distinguish the signal from random noise (I'm guessing).
 


It's not a symmetrical square wave: the duty cycles is about 81%.
 
 Now I just need to think of something useful to do with the good sensor I took out of the car 🙂 
 

Friday, 29 August 2025

Getting UTF-8 from PHP and MySQL

This turned out to be more annoying problem than I expected. 

Consider this MySQL table:- 

 MariaDB [demo]> show create table users\G
*************************** 1. row ***************************
       Table: users
Create Table: CREATE TABLE `users` (
  `name` varchar(64) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
1 row in set (0.000 sec)

MariaDB [demo]> select * from users;
+------------------------+
| name                   |
+------------------------+
| Éámónn McGóníglé       |
+------------------------+
1 row in set (0.000 sec)

Note all of the accented characters (called "síneadh fada" or just "fadas" in Irish), which are about to cause us some headaches.

Now consider this bit of PHP code which retrieves names from that database:- 

<?php
$dsn = "mysql:host=localhost;dbname=demo";
$dbh = new PDO($dsn, "root");
$stmt = $dbh->query("SELECT name FROM users");
$res = $stmt->fetch(PDO::FETCH_ASSOC);
print($res['name']."\n");
 

The result is not what you might expect:-

$ php demo.php 
nn McGlamonn 

All of the accented characters have been lost and there is clearly some other weirdness going on. In a web-browser, there is a weird "placeholder" character everywhere an accented character should be:-


What's going on?  The charset of the database table is set to "UTF-8" and the "Content-type:" header of the response coming from the web server is "text/html; charset=UTF-8", just as it should be.  So why the mess?  

StackExchange proposed all sorts of solutions, but the one that worked was a change to the MySQL connect string, adding a ";charset=utf8" suffix:-

 <?php
$dsn = "mysql:host=localhost;dbname=demo;charset=utf8";

With that small change...

eamonn@redranger:~/public_html$ php demo.php 
Éámónn McGóníglé

Credit where it is due, the Stack Overflow post that helped was https://stackoverflow.com/questions/4475548/pdo-mysql-and-broken-utf-8-encoding/21373793#21373793

 One nuance is that

 <?php
$dsn = "mysql:host=localhost;dbname=demo;charset=utf8mb4";

 ...might be a better choice.  Apparently, in MySQL's world, "utf8" admits only three-byte UTF-8 characters (so not full UTF-8) while "utf8mb4" allows for four-byte UTF-8 characters.  Sure enough, if I try to insert a name with a four-byte UTF-8 character into the database with a charset of "utf8", it fails:-

MariaDB [demo]> insert into users values ("Smiley McSmiley 🙂");
ERROR 1366 (22007): Incorrect string value: '\xF0\x9F\x99\x82' for column `demo`.`users`.`name` at row 1

So I recreate the database table with a charset of "utf8mb4":-

MariaDB [demo]> create table users  (name varchar(64)) charset utf8mb4;
Query OK, 0 rows affected (0.214 sec)

MariaDB [demo]> set names utf8mb4;
Query OK, 0 rows affected (0.000 sec)

MariaDB [demo]> insert into users values ("Smiley McSmiley 🙂");
Query OK, 1 row affected (0.046 sec)

MariaDB [demo]> select * from users;
+----------------------+
| name                 |
+----------------------+
| Smiley McSmiley 🙂     |
+----------------------+
1 row in set (0.000 sec)

However, the PHP script fails to crack a smile:-

$ php demo.php 
Smiley McSmiley ?


 But if I change...

<?php
$dsn = "mysql:host=localhost;dbname=demo;charset=utf8";
 

...to...

<?php
$dsn = "mysql:host=localhost;dbname=demo;charset=utf8mb4";
 

 ...it's smiles all-round:-

$ php demo.php 
Smiley McSmiley 🙂


 

Another solution which is more trouble and less good is to use the PHP call mb_convert_encoding.  I did get it to work for accented characters.  The trick is to convert the ISO-8859-1 character that MySQL returns (without the ";charset=utf8" specified in the connect string) to a UTF-8 character. This works for accented characters such as á, é etc. that have a single-byte mapping in ISO-8859-1, but not for 🙂.  UTF-8 is a better way to do it.

 

 

 

 


Wednesday, 14 August 2024

JavaScript Destructuring Assignment Gotcha

This wasted several hours on me. I'll explain exactly what I was doing that got me into this mess a little later, but the bare essence of the problem that I ran into was this:-

let a = "this is a" let b = "this is b" [a,b] = ["foo", "bar"] console.log(a) console.log(b)

What would you expect it to output? I expected:-

foo bar

That's not what I got, though:-

$ node foo.js /home/eamonn/tmp/foo.js:4 [a,b] = ["foo", "bar"] ^ ReferenceError: Cannot access 'b' before initialization at Object. (/home/eamonn/tmp/foo.js:4:4) at Module._compile (node:internal/modules/cjs/loader:1358:14) at Module._extensions..js (node:internal/modules/cjs/loader:1416:10) at Module.load (node:internal/modules/cjs/loader:1208:32) at Module._load (node:internal/modules/cjs/loader:1024:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12) at node:internal/main/run_main_module:28:49

The error-message made no sense to me. To fast-forward past lots of tearing-out-of-hair, changing the code to this (note the addition of the ";" on the second line):-

let a = "this is a" let b = "this is b"; [a,b] = ["foo", "bar"] console.log(a) console.log(b)

...makes everything work as it should:-

$ node foo.js foo bar

So what's going on? The answer is that the two lines...

let b = "this is b" [a,b] = ["foo", "bar"]

...are actually parsed as:-

let b = "this is b"[a,b] = ["foo", "bar"]

Note that b is being used to to try to dereference the "array" that is the string "this is b". But, of course, b isn't defined at this point so can't be used to dereference anything (and in this light the error-message makes more sense).

My JavaScript coding style is was to omit trailing ";" on the basis that they aren't necessary. It turns out that this is only true most of the time. I'm starting to seriously rethink that style now.

What was I doing? I had two "fetch" commands that I wanted to kick off in parallel. I had some processing to do that depended on the results of both of them. Rather than running them sequentially, I wanted to do this:-

(async () => { let first_response let second_response try { // Schedule the two fetches to run asynchronously const first_promise = fetch("http://my.server.ie/firstthing.json") const second_promise = fetch("http://my.server.ie/secondthing.json") // Wait for the two fetches to complete [first_response, second_response] = await Promise.all([first_promise, second_promise]) } catch(error) { ...do something if either of the fetch() calls craps out } // Process the retrieved data const first_result = await first_response.json() const second_result = await second_response.json() process_results(first_result, second_result) })()

But it would fail:-

$ node baa.js ReferenceError: Cannot access 'second_promise' before initialization at /home/eamonn/tmp/baa.js:12:79 [...]

Adding in that semicolon to clear up the "ambiguity" about the destructuring assignment solves the problem just fine:-

(async () => { let first_response let second_response try { // Schedule the two fetches to run asynchronously const first_promise = fetch("http://my.server.ie/firstthing.json") const second_promise = fetch("http://my.server.ie/secondthing.json"); // Wait for the two fetches to complete [first_response, second_response] = await Promise.all([first_promise, second_promise]) } catch(error) { ...do something if either of the fetch() calls craps out } // Process the retrieved data const first_result = await first_response.json() const second_result = await second_response.json() process_results(first_result, second_result) })()

...and the code works as it should

References

I got the clue I needed from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment. There is a particular sentence buried in there:-

If your coding style does not include trailing semicolons, the ( ... ) expression needs to be preceded by a semicolon, or it may be used to execute a function on the previous line.

Not exactly my issue, but close enough that it pointed me in the right direction. Thanks, Mozilla Developer Network, you are endlessly useful and informative.

Having seen the light, I did some research into the merits and demerits of relying on automatic semicolon insertion. The (now deprecated in favour of Typescript) Google JavaScript Style Guide is pretty emphatic (https://google.github.io/styleguide/jsguide.html#formatting-semicolons-are-required):-

Every statement must be terminated with a semicolon. Relying on automatic semicolon insertion is forbidden.

That's good enough for me.

Thursday, 25 April 2024

try...catch...finally - Not (quite) what I expected

Consider this bit of code:-

def foo(): print("Called foo()") try: print("In the 'try' block") 10 / 0 except Exception as e: print("Got an exception: ", str(e)) finally: print("In the 'finally' block") print("About to return from foo() at the bottom") foo()

What do you think it will output? It probably won't come as a surprise:-

$ python3 a.py Called foo() In the 'try' block Got an exception: division by zero In the 'finally' block About to return from foo() at the bottom

Now consider this slightly variation:-

def foo(): print("Called foo()") try: print("In the 'try' block") 10 / 0 except Exception as e: print("Got an exception: ", str(e)) return print("In the 'except' block, after the 'return' statement") finally: print("In the 'finally' block") print("About to return from foo() at the bottom") foo()

Note the addition of the return statement to the except block. I would have bet money that it would behave differently: that the return in the except block would return straight out of foo(). and the finally block would not be reached. I would have lost the money:-

$ python3 a.py Called foo() In the 'try' block Got an exception: division by zero In the 'finally' block

The return statement in the except block does not exit foo() immediately: rather it runs the finally block and then returns. I can't claim credit for noticing this: my youngest son got caught out by this behaviour and told me about it.

It got me to wondering if other languages behaved the same way. Let's try it in Javascript:-

function foo() { console.log("Called foo()") try { console.log("In the 'try' block") throw new Error("oops") } catch(e) { console.log("Got an exception: ", e) return console.log("In the 'catch' block, after the 'return' statement") } finally { console.log("In the 'finally' block") } console.log("About to return from foo() at the bottom") } foo()

It behaves in exactly the same way:-

$ node a.js Called foo() In the 'try' block Got an exception: Error: oops at foo (/home/eamonn/tmp/a.js:7:15) at Object. (/home/eamonn/tmp/a.js:22:1) at Module._compile (node:internal/modules/cjs/loader:1198:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1252:10) at Module.load (node:internal/modules/cjs/loader:1076:32) at Function.Module._load (node:internal/modules/cjs/loader:911:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:22:47 In the 'finally' block

Ofcourse, this is all well documented: it's just not exactly how I thought it worked. RTFM, people 😉

Wednesday, 3 April 2024

Antenna Experiments - Reprise

This is a follow-up to my earlier earlier post: if you haven't already done so, I suggest reading that one first.

I caved and bought a NanoVNA. It is a useful piece of equipment for not very much money, if you have an interest in RF.

Anyway, hooking up one of those "DF Robot" 433MHz antennae to it produced some interesting (in that they differ sharply from the previous results) results:-

The maximum return-loss/lowest VSWR occurs at 436MHz. Between 433.5MHz and 438.5MHz, the VSWR < 1.5. This is much closer to the expected values and suggest that my previous attempts to measure antenna resonance using just a spectrum analyser are not to be relied-upon.

Turning to the antenna consisting of a bit of coax with 16.5cm of exposed inner conductor, recall that theory suggested that the resonant frequency of that should be 454.5MHz.  It turns out that reality concurs pretty closely:-



 That red marker is at 452MHz, which is pretty close. It also suggests that the velocity factor of the signal in the coax should not be considered when doing the calculation. It isn't completely obvious (to me, anyway) why not: my intuition suggests that it should. But nothing I have read supports this intuition and now the measurements also indicate that my intuition is wrong. If you know why, please do leave a comment letting me know.

Also, it looks like the "rubber ducky" antenna is a much better antenna than the piece of coax at all of the relevant frequencies.


Sunday, 17 March 2024

Avoiding Arduino millis() Rollover Bugs

If you are using the millis() function to measure time on an Arduino, you might be tempted to do something like this:-

unsigned long timer_expires_at = millis() + SOME_DURATION; while (millis() < timer_expires_at) { [do other stuff while waiting for the timer to expire] } [do stuff that should happen after the timer expires]

For the most part, that will work well. But after around 49.7 days (4,294,967,296 a.k.a 232 milliseconds, to be exact), you might run into a problem. If millis() + SOME_DURATION exceeds 4,294,967,295, it will rollover and your comparison will match prematurely. Of course, you would have to be pretty unlucky for this to occur in practice, but - as Mary Chapin Carpenter put it - Sometimes you're the Louisville Slugger, sometimes you're the ball.

I had plans sketched out for a small C++ class to handle this elegantly when I decided to do a quick bit of Googling to see if I was reinventing the wheel entirely here and I came across the most beautiful simple, and elegant solution to the problem in https://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover/12588#12588. It is a long, detailed reply and well-worth reading, but the gist of it is to just change the logic very slightly:-

unsigned long timer_started_at = millis(); while (millis() - SOME_DURATION < timer_started_at) { [do other stuff while waiting for the timer to expire] } [do stuff that should happen after the timer expires]

It should be pretty clear how this avoids the problem after ~49.7 days, but it may be less obvious why it doesn't just shift the problem to the other end.  Imagine that your Arduino has been powered on for 30 seconds, so a call to millis() will return a value of 30,000. You want to wait for 40 seconds, so you set SOME_DURATION to 40,000. Subtracting 40,000 from 30,000 in the condition of the while() loop will indeed cause a rollover (30,000 - 40,000 mod 4,294,967,296 = 4,294,957,296), but the result of that rollover will still satisfy the condition in the way we want.

Antenna Experiments

Let me state from the outset that my knowledge of RF and antenna design is very rudimentary. I basically consider it to be black magic.

With that out of the way, I read something a while back that made me wonder if it was possible to measure antenna resonance with a track-generator equipped spectrum analyser.  In particular, with two identical antennae - one connected to the track-generator output and one connected to the spectrum analyser input - would there be an obvious peak in response at the antenna's resonant frequency.  That is an experiment worth trying.  

Here is the initial setup:-


 It is sweeping from 0 - 1.5GHz and there is already a very obvious peak.  Looking closer:-

The antennae are intended for use with DFRobot APC220 radios which have a frequency-range of 418-455MHz.  So in the ballpark, even if the measured peak is kind of low.  There is a significant difference between performance at the two ends of that frequency range, though. Reducing the frequency span on the spectrum analyser from to 418-455MHz:-


There is nearly 15dB difference between 418MHz and 455MHz.  Still...I can't say for sure what the resonant frequency of the antenna is actually supposed to be: perhaps it is 407.5MHz and the result is completely valid.

For the second test, I replaced the both antennae with a simple monopole consisting of a piece of coax with 16.5cm of the shielding stripped back.  This should resonate at a wavelength of 4 × 16.5cm = 66cm.  Assuming the speed of light is  3 × 108m/s, that should be equivalent to 454.5MHz.  Let's see:-

Far from conclusive. The peak is at 152.5MHz, way over on the left.  There is a second peak at about 415MHz.  Zooming in to the 418-455MHz range:-


There is no sign of a peak of any kind near the resonant frequency.  

My "assumption" above about the speed of light wasn't entirely tongue-in-cheek.  The headline figure of 3 × 108m/s is the speed of light in a vacuum. Signals travel through coax cable significantly slower than this.  According to a datasheet for RG179 coax that I checked (I think that the kind of coax I have), the velocity factor is 0.7.  That would put the speed of the signal propagating through that coax at closer to 2.1 × 108m/s and - if the wavelength-to-frequency computation should take this into account - would put the resonant frequency at 318MHz.  I have no idea if the velocity factor should be applied here or not: if you know, please leave a comment.  There is certainly no bump in the output of the spectrum analyser at or near 318MHz that would give a clue either way.

Finally, I replaced the two coax monopoles with two lengths of wire, balanced precariously in the SMA connectors:-


The pieces of wire are identical and are both 10.5cm long. Looking at them as 1/4 wavelength monopoles and applying the equation, their resonant frequency should be 714MHz.  It doesn't appear to be:


There is indeed a strong peak, but it is nowhere near where it is supposed to be.  Interestingly, if I apply the velocity factor of 0.7, things look a lot better: that would imply a resonant frequency of 500MHz.  But I have no reason to believe that the velocity factor of a scrap of wire is 0.7, even if I knew that it was supposed to be included in the calculation.  I'm just fudging numbers here.

I got a similar result with 16.7cm of the same wire:


A good strong peak at 345MHz where is nowhere near the predicted 449MHz, but in the right ballpark if I apply the magic velocity factor of 0.7.

If you read down this far hoping for a satisfying conclusion and some enlightenment then I can only apologise for the disappointment you must be feeling.  I promise you, I feel your pain.

Nothing in any of this has done anything to shake my conviction that RF and antenna design is a black art.