Wednesday, 6 March 2019

Powering your Pi

This blog is a presentation covering two things.
First 10 minutes, I talk about a problem with the resistance of the USB power leads, which may be strangling your Pi.
Then I go on to a specific use case where I use the MCP3008 to monitor battery voltage and current for the purpose of monitoring a Sealed Lead Acid (SLA) backup battery, which is providing a backup power supply for a Pi project.

My previous blog entry is a presentation about the MCP3008 Analogue to Digital Converter on Raspberry Pi, and you might want to check that out first if you want a deep dive into the MCP3008. In the presentation below, I describe how to use the MCP3008's differential mode (something I somewhat dismissed in the previous presentation), and how it can operate slightly outside the power supply of the chip, which is particularly useful for current monitoring.

This is a presentation I gave at Raspberry Pint in London on 29th January 2019. I would like to thank CodeNode for making the video available.

Friday, 8 February 2019

MCP3008 Analogue to Digital Converter on Raspberry Pi

The Microchip® MCP3008 Analogue to Digital Converter [Datasheet] is commonly used to add analogue (or analog for US readers) inputs to the Raspberry Pi. It is very easy to connect to the Pi using an SPI bus on 4 of the Pi's GPIO ports. There is plenty of software support for it, but it is also easy to bit-bang from a program if you want to drive it entirely yourself.

This is a presentation I gave at Raspberry Pint in London on 24th April 2018 on using the MCP3008 on the Pi, including a live experiment to see how fast I could make it run (how many samples per second). My next blog entry looks at an application design which includes an MCP3008 used in a novel way.

I would like to thank CodeNode for making the video available.
I do not represent or speak for Microchip®.

Monday, 4 February 2019

Rasperry Pi as a Replacement Controller

I build lots of Raspberry Pi based controllers, and sometimes retrofit them into existing appliances. This can give the appliances a new lease of life, and add a bunch of features they didn't have beforehand.

This is a presentation I gave at Raspberry Pint in London on 27th February 2018 on two retrofit controller projects, and I would like to thank CodeNode for making a video available.

Monday, 10 November 2014

Pedestrian Crossing



What you see here is a working model pedestrian crossing, specifically a Puffin Crossing as is increasingly being used in the UK. I built this as a Christmas present in 2008 for a younger member of the family, who had just got very interested in traffic lights, and was starting to understand what the red man and green man at the crossing mean. This seemed like too good an opportunity to miss for something both enjoyable and educational, and that's not just for the young recipient, but also for me in building it! It was a pretty instant hit with the recipient too, and he's spent the days since playing with it.

Design and Construction

Pelican or Puffin crossing?

Pelican crossings are perhaps better known, but seem to be rapidly being replaced by Puffin crossings. The Puffin crossing has a simpler arrangement of red and green man signals, i.e only on the push button unit, and not distant across the road. This also makes for an easier design for a single pole model, as a representative sample of all the operational parts can be included on a single pole. On this basis, a Puffin crossing was chosen.

Puffin crossings use regular UK traffic light sequences for the traffic (i.e. red and amber appear together, and there is no flashing amber). Similarly, there is no flashing green man.

However, the flashing green man is one area where I departed from the Puffin crossing, and used the Pelican crossing sequence with a flashing green man phase to indicate finish crossing, but don't start crossing. This was done because the child is likely to come across Pelican crossings as a pedestrian, and having this in the crossing sequence is a useful education, even if not 100% accurate. In practice, a Puffin crossing (unlike a Pelican crossing) doesn't provide visible red and green men whilst crossing, so it has no need of such an indication.

Puffin crossings monitor the progress of pedestrians across a crossing rather than just operating for a timed period like Pelican crossings. However, in the context of a model toy, timing is used, and there is no sensing of positions of pedestrians, unlike the real thing.

Control

A simple PIC microcontroller is used for the control. The PIC is able to drive 25mA directly from its outputs and includes internal reset and clock source, so other than a few resistors to limit LED current and provide pullups/downs, no additional control components are required.

Sequence of crossing:

1
2
3
4
5
6
1

The crossing stays at step 1 until the crossing button is pressed. It then stays at step one for a further 5 seconds, so child doesn't get the impression a change of lights will happen immediately. In the toy, the remaining steps then follow through on different timers, until we get back to step one. During step 4, there is also a beep - beep - beep - beep sound, like a real crossing. Pushing the button after step 4 will result in the sequence going around again when it gets back to step 1. As mentioned earlier, we also have a step 4a which is the flashing green man, although this is strictly not correct on a Puffin crossing.

Construction

All the parts used are standard electronic contruction parts, except for the pole which is a plastic coated steel tube which is part of a net curtain rod, and handily includes a plastic knob on the end which looks a bit like the top of a traffic light pole. The plastic knob seemed to be firmly attached (an important consideration for a child's toy).

The lights are all LEDs. Standard 10mm ones are used for the traffic lights. The red and green men use clear 5mm LEDs which light a white reflective surface behind cutouts of the red and green men profiles.

A sounder produces the beep - beep - beep - beep sound during the [steady] green man crossing period. This is set to a low volume so repeated operation of the toy doesn't annoy the parents!

A battery compartment is fitted inside the case (screwdriver required for access, as is normal with toys for the young). D cells were chosen to give long battery life and to provide a good weight in the base to prevent tipping over when the pedestrian button is pressed.

I only had about 6 hours to design and build the unit. This included the electrical breadboarding to make sure the PIC could drive the LEDs without any buffering, and selection of current limiting resistors to give matched illumination from the different coloured LEDs, not to mention writing the software. Also, looking up some parts of the Puffin crossing specification to get the sequence right.

One feature I designed, auto power-off, I didn't initially have time to breadboard and implement in the hardware (although it is in the software), so that got missed out, and a separate on/off switch had to be added, much as I wished to have got away without one. A couple of years after first constructing the pedestrian crossing, I got the unit back for a day, and added the auto power-off hardware, allowing the on/off switch to be removed.

In Use

I noticed a small bug in the software during the first day, and reprogrammed the PIC to fix that after the little one had gone to bed. Another issue I noticed was due to not using a pullup in the PIC's reset line - it sometimes took up to 3 seconds to come out of reset when powered-up, which is something that didn't happen on the breadboarded version. I fixed this too.

I was concerned about the strength of the bond between the pole and the base-box. All I had time to do was to superglue it, and I would have preferred to do something stronger. A 2½ year old doesn't have a good understanding of fragility. Having said that, it's been carried around, dropped, hit with a hammer from another toy, and it stayed in one piece for a couple of years, but the bond did eventually break, so I modified a small brass plumbing fitting to fix the pole to the base.

It was certainly well appreciated by the child, and that's the main thing. A few years later, he took it to school to show the rest of the class.

Friday, 15 January 2010

AC Power - Power Factor Explained

I originally published this on blogs.sun.com, but that site is now gone so I have published it again here.
You will need your browser's Java plugin enabled to use the interactive demos below.

From time to time, I have been asked to explain Power Factor, most commonly in connection to computer power supplies or compact fluorescent lamps. With cheap plug-in power meters now being easily available, many more people are becoming interested in this field, and trying hard to remember what (if anything) they were taught about Power Factor too many years ago! Whilst you may have remembered that PF = cos(Φ) (with Φ being the angle of phase shift of the current relative to the voltage). It's less well understood why a switched mode power supply (such as those used in a computer, or a compact fluorescent lamp), which has almost no phase shift, can still have a low Power Factor.

Well, the real definition of Power Factor is given by:

Watts
PF = 
VA

where VA is the RMS Voltage multiplied by the RMS Current drawn by the load.

The more familiar PF = cos(Φ) formula is just a simplification for the case of a phase shift only, but phase shift isn't the only reason for low Power Factor.

Below, I investigate 3 causes for low power factor; Phase Shift, Phase Control (Light Dimmer), and Switched Mode Power Supplies. You can also see the effect low power factor has on the electricity supply. I have avoided the detailed mathematical proofs, and instead provided demos you can tweak with the mouse to see the resulting changes in Power Factor.

Phase Shift

This demonstrates the effect of a phase shift in the current drawn by a load. The demonstration is a 100W load running on 240V.

Initially, there is no phase shift (Phase Angle = 0). This represents a resistive load, and it can be seen that the VA is the same as the 100W Power dissipated by the load. The red area shows the energy taken from the supply.

Drag the Current waveform side to side to generate a phase shift, and see what effect it has. This demonstration keeps the load dissipating 100W, but what you'll see is that the Current increases. When the current shifts, there are periods when it's flowing the wrong way for the applied voltage, and is thus feeding energy back into the supply - this is shown by the green areas. To compensate for these green areas, additional energy is taken from the supply, so the red area increases by exactly the same amount. What's happening is that the load is drawing more energy than it needs to generate 100W during the red time, storing this excess energy, and then feeding it back during the green time. This extra energy flowing back and forth between the supply and the load results in an increase in current flow, over and above that required for the 100W which the load is dissipating. Cables, fuses, and supply transformers must be sized to handle this excess current.

VA

If we simply estimate the power consumption by multiplying the Voltage and Current including the excess, this will be an over-estimate, but this value is useful for other purposes and is known as the VA (i.e. the product of volts and amps). The VA rating is used to calculate size of cables, fuses, and the supply, since these have to handle this excess current.

True Power

The True Power consumption is obtained by measuring the energy used by the load over time (which is fixed at 100W in this example). This is represented as the red area minus the green area in this demonstration.

Power Factor

Power Factor is defined as the True Power consumption divided by the VA. This is 1 for a resistive load where power and VA will be equal, but drops to less than 1 for a load with a phase shift.
You need to enable Java to see this applet.
Drag waveform with the mouse to change phase the angle

Phase Control (Light Dimmer)

This demonstrates how a Phase Control light dimmer works. This is a standard light dimmer used for filament lamps.

The light dimmer uses a Triac semi-conductor switch. Each half cycle, the triac switches off when the current through it drops to zero. Later in each half cycle, the triac is switched on. By varying this switch-on position in the cycle, the RMS voltage supplied to the filament is varied, which in turn varies the light output. Drag the Current waveform left and right to change the triac switch-on position.

Again, this demonstration is for a 100W light on 240V. You will see the Current waveform change as you change adjust the light level, and consequently the power consumption of the light, which is represented by the size of the red area. The Lumen output indicates how much light is given off. Notice that this falls off much faster than the power consumption reduces when you dim a light - this shows how filaments lamps become even more inefficient when dimmed.

The VA and Power Factor are also shown.

You need to enable Java to see this applet.
Drag waveform with the mouse to change phase control angle

Switched Mode Power Supply

This shows the load generated by Switched Mode Power Supplies (SMPSU) which don't contain power factor correction. SMPSUs are used in most electronic items nowadays, such as compact fluorescent lamps, phone chargers, computers, etc. SMPSUs rectify mains into a 360V DC storage capacitor. They only draw current from the mains during the mains peaks, just to recharge the DC storage capacitor, replacing the energy consumed from it since the last mains cycle peak.

Like the first example, we'll consider a load consuming a fixed 100W (typical of a computer). Since the power is the same as the first example, the energy used (the red area) will be the same. However, since the SMPSU can only draw energy during the peaks, this results in high energy demand during this small time, and no energy demand at other times. Thus the current drawn during these peaks is high.

These short large current peaks result in a large RMS current draw, which in turn result in a large VA rating. Consequently, the Power Factor is well below 1. The narrower the peaks, the larger they are - you can change them by dragging left and right on the chart to see what difference this makes, but they are rarely better that a power factor of 0.5.

A large number of SMPSUs has a rather devastating effect on the electricity supply infrastructure. It requires cables, transformers etc to be much bigger than the size they need to be for the True Power consumed, and it can result in flattening of the Voltage waveform. The picture shows the mains waveform at Sun's Bagshot UK site around 2002. The site consisted of a data centre containing a lot of older systems from the 1990s for handling customer support issues, drawing about 2MW. Considerable distortion of the mains waveform is clear.

Consequently, regulations have been introduced in Europe and other areas which require SMPSUs over a few tens of Watts to include Power Factor correction circuits. These work by making the SMPSU draw energy through most of the mains cycle, so it looks more like a resistive load, and the power factor increases to nearly 1. Today's computer systems are much kinder to the mains supply infrastructure than those of the last century.

You need to enable Java to see this applet.
Drag waveform with the mouse to change Current peak width