### Science Communication Master-class

Remember when mainstream media channels used to act dumb and/or condescending round science? Stephen Colbert is among a growing cadre of broadcasters who gets it right. Here’s the best layman’s explanation of gravitational waves I’ve seen.

### Hacking Lego Motors

Lego Technic Power Function motors are nice and easy to hack for robotics projects – in large part because they come with a built in gearbox so you get nice amounts of torque at low speeds right out of the box.

Here’s a quick breakdown of what you need to know. The tl;dr is that you apply a 9V PWM signal to the two inner cores of the ribbon cable. The outer two cores are not connected.

I’m using the M Series 8883 motor but there are plenty of alternative choices available. I’ve not tried them but I would assume they all use the same pin-outs.

The motor is supplied attached by a four-way ribbon cable to a lego electrical connector:

You can either snip this cable in the middle and solder the individual cores or you can sacrifice a simple lego PF extension cable instead and leave your motor assembly intact.

The Lego PF system uses a standard pin-out system across all the products but note that for our purposes here we only need the C1 and C2 lines (the 0V and 9V lines are not connected inside the motor).

Here’s a photo of the motor end showing the pin-outs:

And here’s the pin-outs for the connector end. Which you might need if you’ve just cut your motor cable in half without making a note of the cable orientation…

If you’re interested in the internals, undo the small crosshead screw

then prise off the housing using a small flat-bladed screwdriver trying not to stab your fingers and thumbs in the process

Once that’s off you can pull off the gearbox (which itself comes apart very easily to reveal a set of three planetary gears):

Finally you can slide off the inner housing to reveal the motor itself and the small smoothing capacitor.

You can then see that the outer two cores (0V and 9V) are not connected to the motor internally. So, you control the motor by putting 9v on C1 and 0v on C2 to spin the motor anti-clockwise as seen from the outside, or alternatively put 0v on C1 and 9v on C2 to spin it clockwise as seen from the outside. To control the speed, apply a 0 to 9V PWM signal of the required duty cycle on the logic high control line. In terms of current draw, my M series motor pulls about 70mA when free running and up to about 500mA when I really load it hard. Bottom line – you need a controller circuit to run this, you can’t just hook it up to a micro-controller’s digital out lines.

### Pre-assembled Test LEDs

Here’s a nice little idea I saw somewhere a while ago and have just got round to making. It’s just an LED in series with a 470Ω resistor connected to a pair of 0.1″ pitch posts. Whenever you need to quickly check a signal on a breadboard you can just plug one of these babies in and you’re done.

### Baby’s First Scope

Much as I’d love to have a proper oscilloscope I’m wise enough to know that spending a couple of hundred quid on a cheapo version is probably a bad idea. This is a tool that warrants some significant investment. Or alternatively pretty much none.

I’ve just bought and built a little Jyetech DSO138 kit oscilloscope and whilst the specs are pretty pitiful, for £20 it’s a pretty good piece of kit.

The main features are as follows:

• Analog bandwidth: 0 – 200KHz
• Sampling rate: 1Msps max
• Sensitivity: 10mV/Div – 5V/Div
• Sensitivity error: < 5%
• Vertical resolution: 12-bit
• Timebase: 10us/Div – 500s/Div
• Record length: 1024 points
• Built-in 1KHz/3.3V test signal
• Waveform frozen (HOLD) function available
• Save/recall waveform

So, this thing is not going to cut it for high performance professional applications but if you’re learning electronics and playing with RC circuits and 555 timers then it’ll definitely get the job done for you.

Building the kit requires just basic soldering techniques but some of the parts are quite small, and there’s a lot of them, so it’s not a kit for a first time soldering experiment. It also comes in two flavours, one with the surface mount components already done and one where you need to solder them yourself. Surface mount soldering is a challenge so avoid this version if you’ve never done it before.

While I could go into great detail about calibration and how well it responds and all that jazz, it frankly doesn’t matter. If you’re buying this kit, and you have any kind of common sense, you’re not looking for or expecting that level of sophistication. At the end of the day it lets you see reasonably robust, reasonably slow signals in reasonable situations. Job’s a good ‘un.

### Why Should I Not Use Pointers?

So if pointers are so great, why not only use pointers? For two reasons, both of which are consequences of the fact that pointers are a method of indirection. Indirection is, at heart, adding a step to the set of steps needed to get at the information you need. This makes the process of getting that information more complicated.

With that complexity comes, firstly, increased risk – you have to manage memory yourself and this is fraught with peril and requires both experience and discipline. The object has to exist somewhere for a pointer to have something to point to, so you now have three things to manage – the pointer, the pointee and the relationship between them.

Secondly, the added complexity of indirection brings inefficiency. This can be inefficiency in terms of CPU cycles or in terms of memory usage. Pointers are often an unnecessary overhead e.g. for(i=0; i<10; i++) would be overly verbose if written using pointer notation. If you’re just dealing locally with simple built-in types that don’t take up much space then there’s often little to be gained by using pointers. It’s only when you start trying to pass things around, particularly large things, that the overhead of indirection becomes smaller than the cycles or space wasted by not using it.

### Avoid Passing Large Chunks Of Data

Because of C’s pass by value semantics, passing data into functions using parameters requires pushing and popping that data onto and off the stack, byte by byte. Using return values to pass data out of a function back to the callee also requires copying the data onto the stack.

If you’re just passing or returning an int, then this is no big deal, but if you’re passing big data structures then all that copying can soon mount up.

The solution is to pass pointers into the function instead:

Using a pointer means passing only a few bytes rather than the whole structure. For a large array for example, instead of saying “here’s the contents of the array”, pointers allow you to say, “here’s where you can find the array already in memory”.

### Why Does My Function Not Update My Variable?

So, as a result of some aberrant brain misfiring you’ve decided to start learning to code in C. You pretty quickly come across the idea of functions and your mind is blown.

Then you try to write a function and all is not well. Not well at all. In fact things are looking a bit peaky. You’re not getting the output you expect. Again. Gah. Why was it that you chose C again?

After a stupidly long time spent debugging you finally narrow down the problem to what is obviously nonsensical behaviour. C is clearly broken and you’ve just discovered a fatal flaw that has gone undetected in it for the past fifty years.

This classic and frequent problem encountered at some stage by pretty much all newbies (unless of course they cheated and read the manual) is pretty much universally heralded by “WTF is my function not updating my variable?”

C passes parameters by value – it makes local copy variables of parameters passed into functions. Passing in a pointer means you get a copy of the pointer but that copy is still pointing to the original memory. Pointers are a way to bypass C’s ‘pass by value’ mechanism. Sometimes I want changes I make to be reflected in the source of the data so I need to use reference semantics through pointers. Sometimes I want to manipulate data independently of the original data so I use copy or value semantics. The difference may appear subtle but it is important – a classic and frequent problem encountered by newbies is “Why does my function not update my variable?”

### Pointers As Function Parameters

Suppose you’re writing a function to double a given number. You might naively assume that this would be a reasonable solution:

If you run it you’ll find number remains unchanged. This is, you might be surprised to discover, by design. It’s down to C’s pass by value semantics.

Instead, use referential semantics as follows to allow the doubleIt function to have access to the number variable.

If you run this you’ll find this function works as you would hope and does indeed double the number passed to it.

### How To Avoid Using Globals For Information Passing

If you’ve run into the classic noob C programmer’s problem of “Why won’t my function update my variable?” then you might have resorted to using a global variable instead of passing the variable into the function as a parameter. Global variables allow ‘write access’ to number from both the caller and the callee, like this,

but global variables are considered bad for lots of reasons. Not least of which is that your function can no longer double anything other than your specific number.

Instead you need to pass number into doubleIt as a pointer parameter.

### How Many Threes Are There In The Universe?

The obvious answer is “quite a lot”. There’s the three on the front of my house, there are a couple more in my phone number and there’s one on the number plate of the car across the street from me right now. There are also several copies of my house number three, stored in the databases of various shopping sites who have been kind enough to deliver goodies to my home address over the years. Ditto for my phone number threes; and the DVLA, police ANPR, and several insurance companies presumably have copies of the three on my neighbour’s car number plate. Now in some ways these threes are very different from each other – my neighbour’s car has little to do with my phone. And neither of these have very much at all to do with that group of three wildebeest over yonder on the African savannah. And yet, in some ways, some of them are very similar – the pair of threes in my phone number, for example, share quite a lot of context and, other than their position in the string of digits, you’d be hard pressed to distinguish one from the other. You’ll notice though that I’ve not used the digit ‘3’ anywhere in the discussion so far. And yet that’s what’s encoded in Amazon’s database, even though my house is adorned with the word ‘three’ on a little piece of green Lakeland slate. If I happened to live in certain parts of Canada or in France, I might have the word ‘trois’ in place of the word ‘three’. So, not only are there many different threes, there are also many different representations of the same three. What they all have in common though is some notion of ‘threeness’. All threes are clearly distinct from all fours, fives and 3.14159s. That’s what it means to be three. We can take this platonic idea of three and call it the master three. If we only have one master three then it makes sense that all the other threes we have, the real-world threes rather than the platonic, idealised, ‘perfect’ three, can really just refer to the master three to get their threeness characteristic. In fact they probably should do that so that we’re not duplicating information needlessly, and potentially having conflicting ideas of threeness. Threeness is threeness and it only needs to be defined in one place. All the other bits and pieces of information related to the real-world threes are specific to to those threes rather than having anything to do with the general idea of threeness. The encoding format of the bit pattern on my phone’s SIM card, the font and colour of the numberplate, the origin of the stone used to make my house number. None of these things help us understand the threeness of these threes. This idea of a separation of concerns is at the heart of referential semantics. But, apart from the very real benefit of letting me express clearly what I mean, what does it allow me to do in practice? Well, as we’ve seen, I can refer to the threeness attribute of the master three from my own threes, maintaining a strong and secure threeness without risk of corrupting it and crucially, from a practical standpoint, without incurring any extra storage or processing costs.