Arduino Tips & Tricks

Whilst using Arduino devices in our smart home we have developed our own library of code and resolved quite a few issues along the way. This page shows some of the issues we have had to address and explains how we have fixed them.

Ethernet Shield Issues

We have bought a lot of cheap Arduino Ethernet shields on eBay but, nearly all of them have required a fix due to a wrong component being fitted. This issue is well described here. The symptoms are that the Arduino doesn't stay connected to the network and is particularly sensitive to some types of switches. I saw major issues with a TP-Link switch and a complete inability to connect to my network.

I have fixed the dodgy boards by soldering two 100Ω resistors to pins 1 & 2 and pins 3 & 6. This has fixed all issues and all of my Ethernet shields (I have over 20 of them) now work perfectly well.

Arduino Ethernet Shiled

Basically, if the Ethernet shield has an 8-pin surface mount resistor network labelled '511', then you will probably experience problems. This means it is a 510Ω resistor network instead of the specified 49.9Ω resistor network, which is typically rounded up to 51Ω and therefore should be labelled '510'.

Reset Delay

When using an Arduino with and Ethernet shield, it is advisable to use an RC delay circuit to allow more time for Ethernet shield to boot up. I've not seen this issue when using Arduino Uno processors but it I see it a lot with Arduino Mega 2560 processors. The solution is to use a 220uF capacitor and a 220Ω resistor.

Arduino Smart Home Shield

As part of my 'building block' approach to the smart home, I developed my own Arduino shield, to enable me to prototype and build new smart home services and features quickly and reliably. I designed this with the required components onboard to address this issue.

Long Time Periods

Arduino devices are great if you want to measure short time periods but the in-built millis() function uses an unsigned long 32-bit variable, that as a maximum value of 4294967295. This equates to just 1193 hours or 49.7 days, at which point it rolls over. In smart home applications, this really isn't long enough. To get around this limitation, my projects and code work can also work with seconds instead of milliseconds and this allows my code to work over >49,000 days (136 years) without issues.

The code I use to achieve this is:


// Global variables
unsigned long millis_old;                   // previous time in millis
unsigned long rollovers = 0;
unsigned long seconds = 0;

void setup()  
{
}

// Function to track seconds over long time period
void Timers()
{
    unsigned long millis_new = millis();
    if (millis_new < millis_old)
        rollovers++;
    millis_old = millis_new;
    seconds = ((0x100000000 / 1000) * rollovers) + (millis_new / 1000); 
}

// Main Program loop
void loop()
{
    Timers();

    // The rest of my code

    Serial.print("Seconds: ");
    Serial.println (seconds);
}