Main Content

Lost Connection and Data Issues

Lost Connection with Arduino Hardware

Unplugged Hardware

The most common cause of a lost connection is an unplugged cable. Check the cable and make sure that it is firmly plugged in.

Board with Low Data Memory

If your Arduino® board has low data memory, you may lose connection while working with large data sets. If this occurs, remove all libraries that you are not using from the board to free up some space. For example, create an arduino object with no libraries.

a = arduino('COM3','Uno','Libraries','');

Too Many Device Objects

If you have created too many add-on objects for your Arduino hardware, those objects may use up memory on the board and cause a lost connection. Clear all objects and reconnect. Make sure that you use fewer peripheral devices.

Communication Loss When Writing to I2C Device

Make sure that the power line is always connected. With I2C devices, the server code can stop working if the power line is disconnected.

Connection Lost When Using Timer

You can lose connection to your Arduino board when you use it with a timer. The callback timer on your system runs between lines of MATLAB® code. This can cause the timer to issue a call to the device at the same time you issue a read, write, or configuration change on the Arduino board, or click a button in the interface. The operation throws an error with a re-entrancy exception. For example, consider a timer function that polls Arduino pins.

t = timer('Name','PinSniffer','BusyMode','Drop','ExecutionMode','fixedSpacing','Period',1,'TimerFcn',{@pollArduinoPins,a}); 
function tester(src,event,a) 
try 
    value = 1:10; 
    for iloop = 2:13 
        if strcmpi(configurePin(a,iloop),'D3') 
            value(iloop-1) = readDigitalPin(a,iloop); 
        end 
    end 
catch e 
    stop(src); 
    rethrow(e); 
end 

In this situation, you issue a command to configure pins on your Arduino board and read the value.

configurePin(a,'A0','DigitalOutput'); 
value = readDigitalPin(a,'A0'); 

If your read command and the poll run simultaneously, the timer can interrupt your calls before it has completed. This results in a re-entrancy exception.

To work around this situation, stop the timer before you manually issue a command.

stop(t);
configurePin(a,'A0','DigitalOutput'); 
value = readDigitalPin(a,'A0'); 
start(t);

Connection Lost When Using Encoders

You can lose connection between the Arduino board and MATLAB, when the encoder shaft is rotating fast or the A/B signal rate of the encoder crosses the threshold of 17–18 kHz. When one of these situations happens, the interrupts used by A/B channels completely block the serial communication on the Arduino board which also relies on interrupts. Therefore, it can stop the Arduino board from sending a response back to MATLAB, and you see the following error message:

Host and client connection is lost. Make sure the board is plugged in, and recreate arduino and related objects.

However, once the shaft stops rotating or rotates slower, the serial communication works as usual.

Data Issues

Pin Not Receiving Data

Make sure devices that have floating or high impedance pins have proper pull-up or pull-down resistors. Use configurePin to set the mode to pull-up internally.

Pin Not Receiving Proper Signal

Make sure that your pins are properly configured. For example, a pull-up or a pull-down resistor is being used for reading data. Use configurePin to set the mode to pull-up internally.

Strange Data

If your Arduino hardware has low data memory, you may see strange data. If this occurs, delete all libraries that you are not using from the board to free some space. To delete libraries, create an arduino object with no libraries.

a = arduino('COM3','Uno','Libraries','');

Incorrect Input or Output

You can get incorrect readings if you are using the wrong pin. For example, if you are writing to a pin configured as input or reading from a pin configured as a pull-up.