Reverse engineering a wireless burglar alarm, part 3

In part 1, we used some simple tools available to us to look at the modulation and frequency the Response alarms use (868MHz, 2-FSK)

In part 2, we opened the alarm up and looked at the main components in the system (TI CC11xx RF chips with various microprocessors).

Now we are going to try and look at the communication between the microprocessors and the TI CC1150 chips. They have an SPI serial interface, allowing the user to set various registers to control the rich and complex feature set.

I have a number of tools available to me that can sniff SPI. The cheapest is the Bus Pirate. This has an SPI sniffer interface along with a basic logic analyzer. It works well, 90% of the time. If timings are curious or the protocol deviates slightly from normal, it tends to fall over.

I also have an oscilloscope. It is hard work decoding serial though – it doesn’t have any built in protocol analysers.

So I go to my good old friend, the Saleae Logic 8-channel logic analyser. I have had this a number of years and it has rarely let me down. It’s simply brilliant for dealing with embedded systems, and the software is very easy to use and cross-platform.

I’m going to work with the door sensor initially – they are the cheapest and simplest sensors. I was planning on selling unopened sensors, and the PIRs fetch decent prices on ebay (as an aside, the provenance of alarm detectors on eBay is questionable – could this be an attack vector?).

The RF daughterboard has a 8 pin header. We need to work out which is ground, and then we can safely connect the Saleae Logic. We do this using a continuity tester between the battery connector and pins. It looks like pin 1 is ground.

multimeter

Another pin will be the supply, but at this stage, it doesn’t really matter which. I just need to confirm it is <5V to avoid damaging the logic analyser. I connect the board to the power supply and confirm that no voltages are higher than 3V, regulated down from the two CR2032 batteries.

Small wires are soldered to the pin header to make connecting the logic analyser easier.

logic

We then start the logic analyser with no triggering and press the test button on the contact.

DC_initial

Excellent – this looks very much like we have data!

I could at this stage buzz out the pins to the CC1150 chip to work out what the pins do, but the daughterboard is soldered down so the chip is hard to access. I’d rather just have a guess at the pins – we know it is SPI after all.

SPI requires the following pins:

  • CS – chip select, generally active low.
  • SCLK – the clock for the data.
  • MOSI – master out, slave in data.
  • MISO – master in, slave out data.

CS is nearly always going to be the slowest changing pin – let’s zoom in to the beginning of the trace and see what we have:

Chip Select

From this, I’d wager that CS is channel 0.

Now, to look at this trace, it would be easy to assume that channel 3 is a clock – look how regular it is! However, in SPI, because we are transferring at least 8-bits of data, we need at least 8 transitions each time CS is asserted. Channel 3 isn’t nearly fast enough.

What about channel 4 being the clock? The pulses look to short (and on some traces, they are very irregular as well!):

DC_clock2

But if we look a little closer, we can clearly see 8 pulses per CS. There is no requirement for the clock pulses to be regular and they also don’t need to have a 50% duty cycle.

The two other channels with changes are 3 and 5. One will be MOSI, the other MISO – we aren’t really bothered which right now as it won’t have any impact on decoding the data.

I’ll now set up the Saleae Logic’s protocol decoder for SPI on these channels. There are a number of configuration settings, let’s leave them on the defaults and see what happens – it will throw errors or produce obviously squiffy data if incorrect.

data_mmm_mmm

The little orange and green boxes represent decoded data – we just need to zoom in to see what that data is now. I can then export this as CSV to examine in an external program. We have no errors, so we can check if the decoding has worked by examining the packets against the datasheet.

Time [s] Packet ID MOSI MISO
0 0 0x30 0xFF
0.0001085 1 0x01 0xFF
0.00018025 1 0x0B 0xFF
0.00024925 2 0x02 0x0F
0.00032075 2 0x0C 0x0F
0.00038925 3 0x03 0x0F
0.0004615 3 0x07 0x0F
0.0005305 4 0x04 0x0F
0.0006025 4 0xD3 0x0F
0.00067225 5 0x05 0x0F
0.00074475 5 0x91 0x0F
0.00081325 6 0x06 0x0F
0.000886 6 0xFF 0x0F
0.00096775 7 0x08 0x0F
0.00103925 7 0x12 0x0F
0.00110775 8 0x09 0x0F
0.00118 8 0x00 0x0F
0.00124725 9 0x0A 0x0F
0.0013195 9 0x00 0x0F
0.001408 10 0x0D 0x0F
0.00148075 10 0x21 0x0F
0.00154925 11 0x0E 0x0F
0.001622 11 0x65 0x0F
0.0016915 12 0x0F 0x0F
0.00176475 12 0x6A 0x0F
0.00183425 13 0x10 0x0F
0.00190625 13 0x87 0x0F
0.00197525 14 0x11 0x0F
0.002048 14 0xF8 0x0F
0.0021175 15 0x12 0x0F
0.00218975 15 0x00 0x0F
0.00225725 16 0x13 0x0F
0.00232975 16 0x22 0x0F
0.00239825 17 0x14 0x0F
0.002471 17 0xF8 0x0F
0.0025405 18 0x15 0x0F
0.00261325 18 0x50 0x0F
0.00269225 19 0x17 0x0F
0.0027655 19 0x30 0x0F
0.002834 20 0x18 0x0F
0.002906 20 0x18 0x0F
0.00306925 21 0x22 0x0F
0.0031415 21 0x15 0x0F
0.0032105 22 0x23 0x0F
0.00328375 22 0xE9 0x0F
0.00335325 23 0x24 0x0F
0.0034255 23 0x2A 0x0F
0.0034945 24 0x25 0x0F
0.00356725 24 0x00 0x0F
0.0036345 25 0x26 0x0F
0.00370725 25 0x1F 0x0F
0.00379825 26 0x29 0x0F
0.003871 26 0x59 0x0F
0.0039405 27 0x2A 0x0F
0.00401325 27 0x7F 0x0F
0.0041055 28 0x2D 0x0F
0.00417875 28 0x35 0x0F
0.00424825 29 0x2E 0x0F
0.0043215 29 0x09 0x0F
0.00438775 30 0x7E 0x0F
0.004463 30 0x00 0x0F
0.00451425 30 0x0B 0x0F
0.00456675 30 0x25 0x0F
0.0046195 30 0x68 0x0F
0.00467225 30 0x60 0x0F
0.004725 30 0x86 0x0F
0.0047775 30 0xCC 0x0F
0.00483075 30 0xC3 0x0F
0.0049125 31 0x35 0x0F
0.01802275 32 0x36 0x2F

This is exactly what I would expect to see when a micro is controlling a CC1150. Let’s work out what is going on.

We are just going to lock at MOSI – the packets the microcontroller is sending to the CC1150.

The first packet is an 1 byte packet, where 0x30 is sent. If we search for 0x30 in the datasheet, we find out that there are a series of 1 byte packets called “strobe commands” on page 42, table 25 . They all trigger changes in the internal state machine. 0x30, SRES, reset – fairly obvious meaning!

strobes

Next we have a series of 2 byte packets. You can see the first byte of each packet is generally just incrementing one by one. Again, we look through the datasheet and on page 43, table 26, we have a list of the configuration registers. Notice how we skip some numbers – 0x0C, 0x0D – this is because they are only used for reception, not transmission, and the CC1150 is a transmitter only.

I could go through these one by one, explaining how they are configuring the radio. But let’s just look at the most important ones for now, referring to the datasheet.

FREQ2, FREQ1, FREQ0 are stored in 0x0D, 0x0E, 0x0F and are set to 0x21, 0x65, 0x6A respectively. From page 47 of the data sheet, we need to concatenate these (0x21656a) and then use a simple formula to work out the frequency. This gives a result of 868,299,865Hz – almost exactly as we measured.

freq

MDMCFG2, 0x12, contains modulator settings. Bits 6-4 are the modulation format – so let’s look at them. The value written to 0x12 is 0x00,  so bits 6-4 are 0b000. This corresponds to 2-FSK, as we observed before.

DEVIATN, 0x15, tells us the deviation settings (i.e. how far apart the two frequencies will be in 2-FSK). A simple formula is used. Bits 6-4 give us the exponent, and 2-0 give us the mantissa. 0x15 is set to 0x50, so the exponent is 0b101 and the mantissa 0b000. From this we get 50781.25Hz – again, this corresponds to our ~100kHz gap between the two peaks from part 1.

We are now confident that we the SPI traffic we are looking at is correct. Now we need to work out how data is being sent between the two. There are so many different ways this can be done using the CC1150, so we will need to drill into the settings and sniffed traffic in more detail – which we will do in the next part.

Reverse engineering a wireless burglar alarm, part 2

So we have some idea of the modulation and frequency used by the alarm from our initial investigations. That’s only part of the picture really – but at least we know this isn’t using frequency hopping or anything too challenging.

Where to go from here? We take it apart! I’m not even going to power anything up in this instalment – just see what the system is made up of.

The alarm consists of several components…

SL2 official image frrom allaboutelectrics.co.uk

  • A solar powered bell box on the outside wall, with flashing LEDs, PP3 and 6V sealed lead-acid batteries, and a tamper switch.
  • A keypad that mounts inside. This has 3 indicator LEDs, a tamper switch and is powered by a PP3 battery.
  • PIR detectors. These have a single red indicator LED, tamper switch and are powered by a PP3 battery.
  • Door contacts. These have a single red indicator LED, tamper switch, auxialliary contacts, and are powered by two CR2032 lithium batteries.

All use double sided PCBs, which appear well designed and are well made. Quality control stickers are on each board, as are small stickers on microcontrollers that look like firmware versions.

PIR

PIR

What do we see in here?

  • An Elan EM78P418N microprocessor (PDF datasheet). These are essentially Chinese PIC clones that have one-time programmable (OTP) ROM in them. Generally OTP ROM is cheaper than EEPROM. I don’t expect I would have much luck reading the code out as. It might have in-circuit debugging, but the documentation is poor and I have had no luck with other Elan micros.
  • An empty space for the crystal (X1), that indicates the Elan microprocessor is using an internal RC oscillator. This means that the microprocessor is not going to be doing anything requiring accurate timing, whether than be fast asynchronous protocols or waking at specific times. It does mean that at some point they considered using one – why would that be?
  • An empty space for an 8-pin device, U1. There aren’t many 8 pin devices in use these days, only really serial EEPROMS. Pins 1-4 are common and likely ground. The Elan microprocessor has no EEPROM. Optional EEPROMs are interesting – usually the microprocessor will check for the presence of the chip, then check for a “programmed” flag. If the EEPROM is not programmed, it will write the default values and set the “programmed” flag. I might be able to add an EEPROM and see what it could store.
  • A small RF daughterboard, with a TI CC1150 sub-1GHz transmitter (PDF datasheet) and 26MHz crystal. This is one of the CC11xx family – another well known member being the CC1110 SoC transceiver used in the GirlTech IM-ME. They are incredibly versatile and configurable. Importantly, we know that the PIR can only transmit, and not recieve. Many devices used a daughterboard for RF as I believe this can pass certification independent of the whole product and be easily re-used.
  • The rest of the components are the power supply or PIR detector.

Door Contact

DC

  • Another Elan EM78P418N microprocessor. It makes sense to use the same technology across the range if possible.
  • An empty space for a 8-pin SOIC again.
  • No empty space for a crystal – it was never a consideration to use an external crystal for this board.
  • The same RF daughterboard as in the PIR.
  • The coloured wires are my addition!

Keypad

CP

  • Yet another Elan EM78P418N microprocessor.
  • An 8-pin SOIC! HO18 08B1 – not much info on this. Pins 1-4 are grounded so I expect this to be a serial EEPROM.
  • No empty space for a crystal.
  • The same RF daughterboard again. This means the panel can only transmit, not receive. It is not aware of the state of other components,
  • The pin headers and wires are my additions.

Bell box

BB

  • An ATmega88 10MU122 microprocessor (PDF datasheet) – much more exciting (and much more familiar)! These use EEPROM and if the fuses are set right, I can read the EEPROM easily. I always find it interesting when products contain different processor architectures – maintaining two sets of code is an extra burden, so this choice is not made lightly.
  • A cylindrical can crystal with no markings – often these are 32.768kHz crystals for timekeeping. The ATmega88 has a internal RC oscillator which can be quite accurately calibrated, but nowhere near as accurate as an external crystal. Interesting how this is the only component with an external crystal.
  • No EEPROM or space for an EEPROM – probably because the ATmega88 has it built in unlike the Elan micros.
  • A 6-pin populated pin header – Atmel’s in-circuit programming connector is 6-pin. I wouldn’t ever expect anyone to open this and reprogram it though – so why is the header on there?
  • An RF daughterboard that is very similar – expect this time it is a CC1101 transceiver rather than just a transmitter. Interesting! This means that the only component of the alarm system that can receive and transmit is the bell box. This may have implications later.

Summary

So, for the RF side of things, all of the components are using the TI CC11xx chips. These support 2-FSK, fitting in with the findings in part 1. This is nice – I have worked with other systems using two or even three different RF frontends, and it makes things much more complex. We are also quite lucky – the CC11xx chips are very capable of frequency hopping spread spectrum at a high rate. We’re also lucky these aren’t the SoC versions as these have an AES encryption engine which makes strong encryption almost trivial.

Like Adam, I’d rather avoid RF. Luckily, the interface between the CC1101 and CC1150 is by SPI – I can simply sniff the connection between the RF daughterboard and microprocessor to see both the configuration and data. This is a privilege you don’t have if the CC1110 SoC is used – the transceiver and microprocessor are integrated with no easy way to access the config.

Next time, I will look at sniffing the SPI traffic between the microprocessors and RF boards.

Reverse engineering a wireless burglar alarm, part 1

After Adam’s recent post on reverse engineering a wireless doorbell, I thought I would take you through a similar process, but with a different system.

This is a Response SL2 wireless burglar alarm system, purchased from Amazon in late 2011. They seem fairly popular and well-reviewed.

We start our research without even touching the alarm, by using google. Their product page:

Operates on the 868MHz frequency with 20 Bit ID code and 1 million unique codes for added security

 So we already have a hint to where to look in the spectrum. Interesting how they say both “20 Bit ID code” and “1 million unique codes” – it is redundant information, but “20 Bit” sounds technical, so they probably add it for impact.

Notice that a number of keywords haven’t been used here:

  • Narrowband
  • FHSS
  • Spread spectrum
  • Rolling code
  • KeeLoq
  • Encrypted
  • Bi-directional

It’s likely that if the alarm used any of these, they would be making it known.

(As an aside, Friedland did initially tell me the SL series of alarms used rolling code – being a bit free and loose with specs seems to be common in the alarm world…)

DSCF7863

Let’s start off looking at this as if we couldn’t access any of the components. I’ll use my RF Explorer spectrum analyser to take a look at what is going on. The RF Explorer can be used standalone, but if you are next to a PC, the Windows client is really great and much quicker to use.

RF Explorer (wide)

I start by tuning it to 868MHz with a span of 5MHz. This will be wide enough to receive the transmission if it uses spread spectrum, multiple channels, or if the frequency is out. I trigger the alarm, and as you can see I end up with a broad peak at around 868.3MHz. Let’s go in further.

SL2 RF explorer2

Now I can see that I have two peaks, spaced about 100kHz apart. From this static image, you can’t clearly see what is happening over time – is this 2-FSK modulation (where data is encoded as two frequencies), or is it AM on two frequencies (rubbish frequency hopping spread spectrum)? The RF Explorer can’t really tell me – so it’s about time I whipped out my software defined radio

DSCF7865

Why did I start with the RF Explorer? The amplitude measurements are far more accurate and it can show me a much wider range (600MHz) of spectrum all in one go.

This is a TV tuner dongle. Some rather clever people worked out that it has a wideband RF frontend and can be tuned from about 25MHz to about 1750MHz, presenting us with a ~2.8Mbps baseband signal to work with. It is essentially the same as the FunCube used by Adam, but a lot cheaper. It does have some quirks and limitations though – it can be very deaf in certain areas and you see spurious signals due to the super-heterodyne design of the receiver.

SDR#

I can now see the signal from the alarm in a piece of software called SDR# – this not only shows me a spectrum analyser view (amplitude vs frequency), but also a waterfall diagram (frequency vs time). I can see that the two peaks are nearly continuous – I am likely dealing with 2-FSK modulation.

I can record the baseband signal and look at it in an audio editor as well:

Audacity screenshot

This clearly looks like data now.

At this moment, I have several options:

  • Use gnuradio to build a software receiver for 2-FSK. I will still be none the wiser to the protocol, and it will be receive only.
  • Crack open the alarm and take a further look…

Part two will show how I opened the alarm and started to work out what the system was doing….

Bus Pirate power supply oscillations

Earlier today, I saw that Jeff Keyzer (aka MightyOhm) had posted on his blog about power supply oscillations on the Bus Pirate. The Bus Pirate is one of my go-to tools, and 99% of the time it works as I’d hope, so I thought I’d check mine for issues.

Jeff’s SparkFun built Bus Pirate was showing a 15kHz, 150mVpp signal on the 3.3V power supply rail. Surely I would have noticed if mine was doing this?

Ian at Dangerous Prototypes replied to Jeff on twitter:

So I took a quick look at mine, hoping it wouldn’t be the same. I have a v3 Bus Pirate ordered from Seeed Studio many years ago.

It wasn’t the same, but it also exhibited an issue.

Turning on the 3.3V power supply gave me the following trace:

DS0001

So I have fairly strong bursts of noise, sometimes up to 400mVpp! These seem to repeat at about 150kHz.

Zooming in on one of these bursts:

DS0002

It looks like something is ringing at about 20MHz.

It was a very similar story on the 5V rail.

So the quick fix is to whack a small electrolytic capacitor in:

DSCF7855

I did this for the 3.3V regulator. As soon as I had, the severe noise and ringing on the 3.3V rail stopped.

Interestingly, the noise on the 5V rail stopped as well…

I thought I was going loopy. Yet desoldering the cap causes the noise on both to return.

So what is going on here? Are two regulators oscillating and interacting in a bizarre manner? Who knows. I’m just going to put a second cap on the 5V regulator as well.

Does this make the Bus Pirate worthless? No. I doubt the noise would cause problems for most people. I doubt the noise is as serious when load is placed on the regulators. The fix is incredibly quick and easy. It’s still an incredible tool.

Arduino misconceptions 5: you’ll wear out the flash memory

On the ATmega328P and most other Atmel microcontrollers, code is stored and executed in flash memory. Every time you “upload a sketch”, you are communicating with a small piece of code called the bootloader, which then programs the flash with your code.

Flash has a finite number of program/erase cycles – you can only write to it a certain number of times before bits will either be programmed incorrectly or become stuck at 1 or 0. With an ATmega328P, this will render the device unusable unless you invest a lot of time fiddling with the toolchain.

Now and then, someone will either ask “Will I wear out the chip?” or someone will admonish a newbie for so frequently programming the chip.

The reality of it is you are highly unlikely to wear out the flash memory on an Arduino.

Atmel spec 10,000 cycles. I don’t know the maths behind it, but it means they are highly confident a large proportion of chips will reach this level.

If we put that in real terms – if you are a hugely dedicated hobbyist who spends 2 hours each weekday and 8 hours over the weekend on their Arduino, flashing it once every 5 minutes, you will get almost a year of use before the chip could fail.

For a much more reasonable use case of about 8 hours per week, flashing it every 15 minutes, you get 6 years of use.

For the <£5 that the chip costs, this seems entirely reasonable to me.

Further to this – take into consideration that 10,000 cycles is almost guaranteed. Many will get far higher than this. Dangerous Prototypes have a project called the “Flash Destroyer“, which has the sole purpose of performing program/erase cycles on EEPROM to see how far it will go. A 1,000,000 cycle EEPROM got to 11,500,000 cycles before failure.

So that one year could become 10, and the 6 years become 60.

 

 

 

 

Arduino misconceptions 4: the Arduino is obsolete now the Raspberry Pi exists

There’s no doubt the Raspberry Pi is extremely popular, and has been in the media far more than the lowly Arduino. But many users, forums and reddit seem to think that the Raspberry Pi is going to make the Arduino obsolete – after all, why would you spend £30 on an Arduino when you could spend £30 on a Raspberry Pi?

The Raspberry Pi is amazing – it’s a powerful ARM board for not very much. It has design flaws – the power by USB being the main one – but it has found a place, mainly as a media server for geeks.

However, for those looking for an Arduino replacement, it has many downsides:

1. The GPIO pins are only 3.3V tolerant. This locks out vast numbers of 5V add-on boards and peripherals that can be used by the Arduino.

2. The GPIO pins can only sink/source very low levels of current. So you need to add drivers to get decent levels of current. The ATmega328P has much stronger drive levels.

3. The whole board is less tolerant of overload of mistreatment. The ATmega328P is actually a very hardy chip.

4. The hardware peripherals built-in are lacking – timers, ADCs, PWM, pin change interrupts and so on. It may have some of these, but if it does, they aren’t widely documented.

5. If I want to use the ATmega328P in my own project, I can build a clone board for £5. I can’t built a Raspberry Pi, at all, and my tools, soldering and assembly skills are very good.

6. I can run an Arduino-like board for months, even years, on a single AA battery with aggressive power saving. This is just not possible with a Raspberry Pi.

7. Linux is not a real-time OS (RTOS). This may not mean much to a lot of people, but interfacing to the real world can be a real pain when IO and interrupts are not serviced predictably.

An intriguing mix of micros

I’m working with a commercial product at the moment that consists of multiple wireless devices connecting to one another. Most systems I have worked on stick to a single microcontroller or RF frontend, or at least a single family of microcontroller and RF frontend.

Not this one.

Node 1 – A Texas Instruments MSP430F2132 as the microcontroller and an Analog Devices ADF7021 RF front end.

Node 2 – A Texas Instruments MSP430F1111A as the microcontroller and an Infineon TDK 5100 RF front end.

Node 3 – A Renesas H8/3008 as the microprocessor (yes, no integrated flash or RAM) and an Atmel AT86RF211SW.

All components were purchased at the same time, in fact, as part of a kit. They are all manufactured by the same company.

It amazes me that they could even work together. I just can’t understand why someone would architect a system like this. Any ideas?

Arduino misconceptions 3: it isn’t low power enough to be run from battery

People build their projects, then want to battery power them. Their solution is to use a large battery (e.g. a 12V lead acid), connected to the Arduino external power input. The battery lasts mere days, and they become frustrated and move on to processors perceived as low power, like the MSP430 and ARM Cortex M0 series.

What if I said you could run a ATmega328P, RF transceiver and sensors from AA batteries for months at a time? A lot of people just won’t believe it, thinking the ATmega328P is a dated, power hungry chip that needs an ugly wall wart for power.

So why are people struggling with battery power?

1. The standard Arduino board accepts an external power input of between 7-12V, which then passes through a delightfully named NCP1117ST50T3G low dropout linear regulator to get it down to 5V for the rest of the board. If you are using a 12V battery via this regulator, and drawing a measly 50mA, you end up burning 0.35W of power in the regulator and the Arduino only using 0.25W! That linear regulator is an evil thing.

2. Most Arduino boards run at 5V. The ATmega328P runs fine at 3.3V and even down to 1.8V. At 3.3V, the chip uses ~40% of the power, and at 1.8V, it uses ~10%. Massive gains! 1.8V can make interfacing to other systems a bit awkward, but 3.3V is generally fine.

3. Most Arduino boards run at 16MHz by default, but a lot of the time you don’t need to run that quickly. By dropping to use the 1MHz internal oscillator, you reduce the power consumption 8 times. If you go further, you can use 128kHz oscillator and the power consumption drops 70 times!

4. A lot of Arduino code uses delays() and never sleeps. The ATmega328P has a sleep mode which can easily use less than 10µA – if you use an external 32.768kHz watch crystal, you can get this down to fractions of a µA. Learn about these and use them.

Where do you find out all of this info? It’s at the end of the ATmega328P datasheet.

You can workaround all of these – I thoroughly recommend reading JC Wippler’s posts on Jeelabs in his quest to reduce power consumption on Arduino-like board.

Arduino misconceptions 2: Arduino is “slow”

For the second post about Arduino misconceptions, there is the a common idea circulating that the Arduino is “slow”. Most frequently I hear this in the context of reacting to user input, dealing with multiple sensors, using LED or LCD displays, or acting as part of a control loop. People advise faster microcontrollers such as the ARM Cortex series.

Let’s look at the fundamentals here:

  • The ATmega328P on the Arduino boards runs at 16MHz – that’s 16 million cycles per second.
  • The ATmega328P instructions take between 1 and 3 clock cycles (with the exception of the subroutine related instructions which take 4 or 5). The average is somewhere between 1 and 2 for most compiled C code.
  • We can then infer than the ATmega328P can carry out at least 8 million instructions per second!
  • It’s hard to directly translate these instructions to lines of C code. What looks simple in C can take tens of instructions, and what looks complex can be done in one. 
  • We can still say that the ATmega328P is going to tear through your code at a rate of knots, far faster than most people imagine.

So why do people say it is slow? I’d say the following reasons:

  • It is 16MHz, and most people’s PCs and phones operate in the 1GHz range, so it doesn’t sound like much. The ATmega328P is performing entirely different tasks though.
  • It is 8bit, and most modern processors are 32 or 64 bit. This doesn’t have many implications for projects using the Arduino though (but will be related to my next misconception!)
  • The frequent use of delay() in Arduino code. Delay() causes the processor to just churn – it can’t do anything else whilst it is running. So if you have 4 buttons which are meant to turn on 4 respective LEDs for 2s, the system becomes unresponsive if you use delay() for the 2s.
  • The frequent use of Serial.print() in most code for debugging or status reports. Arduino pre-1.0 uses to block when sending data out using the serial port. That meant an 80 character string output at 9600bps (the default, for some reason), would take over 80ms, during which time the processor could do nothing else! Even now that it uses interrupts, strings still take a long time to output.
  • Slow digitalRead and digitalWrite – these two functions are orders of magnitude (~60 cycles) slower than direct port access (~1 cycle!). Heavy I/O can look slow or latent as a result.
  • Bad code – the Arduino is exceptionally easy to use without understanding any of the underlying concepts of microcontrollers. As a result, code is sometimes simply bad.

A faster microncontroller can mask some of these issues, but without understanding some key concepts (interrupt handling and well structured state machines), you are going to be up against the same wall again in no time.

That’s not to say that faster microcontrollers aren’t needed sometimes – anything with heavy number crunching needs something more – but for a lot of situations an Arduino is more than capable in the hands of a good developer.

Arduino misconceptions 1: need to use external pull-up resistors

One of the first things you need to learn when interfacing switches to microcontrollers is the use of pull-up resistors. These ensure that the inputs to the microcontroller settle in either logic high or low when the switch is not made. They are fundamental, so fundamental that Atmel decided to build-in weak pull-up resistors into the ATmega328P used in the Arduino!

Simple pull-up resistor

Yet a lot of tutorials show external pull-up resistors being used with switches. There’s not really a problem with this – just that it is not required 99% of the time. It might be good to teach the concept, but I regularly see posts on forums where people have connected pull-ups incorrectly, causing them problems. I’ve even seen people confident enough to design a PCB, but have multiple external pull-ups for interfacing to a keypad!

InternalPullUp

So how do you use the internal pull-ups? It is very simple – when the data direction register is set to input, write a high to the output port:

pinMode(pin, INPUT);
digitalWrite(pin, HIGH);

Or, if you have moved away from the Arduino libraries:

DDRx &= ~(1 << pin);
PORTx |= (1 << pin);

What’s the other 1% where these internal pull-ups won’t do? There are two situations:

  1. The switch requires a pull-down rather than pull-up – though this can generally be avoided
  2. You need a strong rather than weak pull-up – sometimes devices draw power from the pull-up, and the 20kOhm internal pull-up is too large.

So, simplify  your circuits and remember this feature!