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!

Strangest boardroom in London?

Every few months or so, I trawl several film location databases to find new places to look round. 95% of the sites are of no interest, and only a few of the remaining 5% can easily be found.

This place, off the Itasca location database, really got my interest with the strange turret like structure – it really looks like prison watchtower or similar.

Itasca_30001_001

So, it looks like a board room in there. Where’s the stairs though? What does that sign say “SLIDE TABLE TO OPEN FIRE EXIT”?

Itasca_30001_009

Oh right, so you actually slide the table apart to get to the stairs…

Itasca_30001_010

And down we go…

Itasca_30001_011

Imagine needing to go for a wee during a meeting.

For those interested, this is the old Fulton umbrella factory, just in the shadow of the Canary Wharf estate. I think it was vacated sometime last year.

Nadine Dorries bends the truth to support her agenda on social media

Nadine Dorries has passed comment on social media and the recent civil unrest. It’s an interesting topic, and one that could be debated for many hours. However, keeping to form, she bases her arguments on untruths and presents opinion as fact.

Her statement:

During 7/7, mobile networks were instantly closed down.

Is false.

The 7 July Review Committee in their report stated:

We subsequently found out that in fact ACCOLC had been activated, by the City of London Police, on the O2 network in a 1km area around Aldgate Station

ACCOLC, according to wikipedia is:

ACCOLC (Access Overload Control) is a procedure in the United Kingdom for restricting mobile telephone usage in the event of emergencies. It is similar to the GTPS (Government Telephone Preference Scheme) for landlines.

This scheme allows the mobile telephone networks to restrict access in a specific area to registered numbers only and is normally invoked by the Police Incident Commander (although it can be invoked by the Cabinet Office). The emergency services are responsible for registering their key numbers in advance.

Also in that report:

The O2 network was closed … at about noon, and remained closed until 4:45pm

That is neither instant (the first bomb was at approx 0850) or across more than one network, or in anything but a very small area.

Also, evidence given at the inquest (yes, this is protectively marked restricted and is on a gov.uk website):

Accolc

Mid-air heist

Browsing the Daily Mail website to find amusing stories about how anyone brown is out to ruin society, I came across an interesting story about a mid-air heist. A passenger on a small plane removed panels from the toilet, climbed into the hold, and stole a large sum of cash in transit. Very Hollywood.

It seems that the plane was a small ATR-42 turboprop plane – which normally only has one toilet. It must have been quite hard for him to monopolise it for the entire journey. I also wonder how he got the tools onboard to enable him to get into the hold – maybe the small airport security isn’t too rigourous? Also, where did he conceal such a large amount of cash when leaving the plane?

I like the idea of stealing high value items from the hold of a passenger plane. I can see many advantages – the cover of engine noise, the delay in being found out, the assumption that once the cargo is on the plane, it is safe. But on how many planes is this even possible? Outside of the Hollywood portrayal of cargo spaces (Air Force 1, Flightplan etc.), how many can be easily accessed from the passenger space?

On a side note, I have remembered how much I love cutaway diagrams:

TV Licensing, bit of a security lapse

I just had the TV Licensing man knock on the door. Their database has been looking at us, because we don’t have a license at this address. We do, however, have a license at our old address (despite which, the letters keep on arriving).

I inspected his ID. Their IDs are crap, and would be almost trivial to forge.

So I told him our old address and postcode. At this point, no names had been mentioned.

He then says, OK, I will transfer it across to the new address. He then turns the palmtop device round to me to show the transfer. I can see our details on the screen, so he clearly has access to the database and is probably for real.

But I have now transferred the license without knowing anything but an address and postcode. Are there going to be any further steps to authorise this process? Probably not.

So, if they knock on your door and you are sans-license, just make up an address and hope for the best…

Full disclosure?

Today I am doing anti-money laundering training. This comprises of
several things:
1. What is money laundering.
2. Why you shouldn’t do it.
3. How to spot money laundering.

Now, prior to today I only vaguelly knew what money laundering is. I
also just knew it was illegal, but not what others could lose from
doing it. Also I had no idea how to spot it.

But now, truth be told, all they have done is give me the information
to launder money successfully.

They have given me the motive – previously I didn’t know how it
disadvantaged people. I just look at the opposite side of it and see
how it would advantage me.

They’ve given me the means – I now know how it works.

They’ve also told me how to spot it, and by extension, how to avoid
being caught.

It really does seem that all the training has achieved is keeping the
honest people honest.

I guess however, when it comes down to it I fall into that group.

Charterhouse Aquatics – poor customer service

I just have to post this review of the customer service I received with an order at Charterhouse Aquatics over Christmas.

I ordered about £150 worth of stuff from them, called to check it was all in stock and could be delivered before Christmas. They said yes, so I placed the order online. It was on next day delivery, I took the next day off.

It didn't arrive.

I called the couriers, who assured me it would be out for delivery the next day. So I took the next day off.

It didn't arrive.

I called the couriers, they said there had been a problem and it would be on the pre-1030 delivery the next day.

It didn't arrive.

I couldn't be bothered to speak to the couriers anymore (Lee Couriers) as they seemed to just lie constantly.

I then spent the next 6 days trying to get a response from Charterhouse Aquatics. Eventually I spoke to someone, and I told them to sort the problem out or cancel the order. The online shopping experience is decided not only by the goods but by the delivery service as well.

On two further occasions the delivery didn't come when promised. One of these days I didn't even bother staying in as I knew they wouldn't be coming.

I contacted Charterhouse Aquatics to say I would contact the card company for a chargeback.

The package arrived the next day. The box was badly damaged, but nothing was broken. An item was missing – it could have fallen out of the damaged box.

I asked for them to send out another, they replied 3 days later saying they would.

A week on, no replacement item, I contacted them again. 2 days later, they reply saying another will be sent out.

This arrives 3 days later.

It was over 4 weeks from the beginning to end.

By far the worst customer service I have had shopping online:

  • Uncontactable by phone and e-mail
  • Unreliable couriers who say they will deliver but don't
  • Bad packaging
  • Couriers damage packaging
  • Missing items
  • Don't seem to care attitude
  • No apology or offer of compensation

I'd hate to see what they are like if I wanted to return an item.