The most common is that the programs and circuits do not work on the first try. This happens even to the most experienced, nobody gets rid of Murphy's Law.
The most frequent faults and also the most difficult to find, tend to be the simplest. They are failures of the type why doesn't this television work? not realizing that it is not connected.
Below is a list of the most common errors so that it can serve as a guide to find those failures that resist and give so much work.
Are you connected?¶
There are multiple versions of this common bug. At this point it should be noted that the check must be done in such a way as to ensure a good connection with complete reliability. It is not enough to see if the cable is in place. It is also necessary to check that there is continuity, it must be checked with a multimeter that the signal arrives and that there are no other unwanted connections, such as a short circuit. Before continuing to search for more complex faults, it must be fully verified that the connection is correct. Below is a list of very common versions of this problem.
Is the power connected and is it the correct voltage?¶
The easiest and safest check is to measure the supply voltage of the circuit with a multimeter. If the circuit has a led to check the presence of power, it will also help, but with a led it is not possible to ensure that the voltage is correct.
In general it is not enough to know that the circuit is connected, because this can be misleading. Imagine a board connected to a USB cable that powers it. Obviously the board is connected, but is the other end of the cable connected? Is the hub to which the cable is connected is itself connected to the computer? Is the computer running? Are any of the wires cut? Are any of the ignition switches not in the on position or stuck halfway? The safest thing is to check the supply voltage in the final circuit.
Is the analog signal connected?¶
As in the previous point, the safest thing to do is to measure the signal level that reaches the circuit with a multimeter. When trying to measure an analog signal, it always measures the same or only noise is received. In this case you have to check several connections:
- Is the ground connected?
- Are the voltage reference signals V+ and V- connected?
- Does the cable from the sensor to the microcontroller have continuity?
- Is there a short circuit that does not allow the signal to arrive?
Is the signal connected to the correct pin?¶
The first input of the RA port is RA0 and the second is RA1. This can lead to confusion at times. Is the signal connected to its input? If there is a confusion in the input/output port, the problems start. Is it connected to RA or RB? On some microcontrollers it is possible to assign the inputs/outputs to the pins by software. Is the input/output pin set correctly?
Is the Pull-Up or Pull-Down resistor connected?¶
Certain outputs need to have a resistor connected to power so that the output can give a signal. This is the case of some comparator circuits such as the LM311 (open collector output) or some special microcontroller outputs (open drain output). The best way to verify correct operation is to check the voltage on the pin with a multimeter. In these cases, a detailed reading of the datasheet can avoid making this mistake.
Is the pin configured as input or output?¶
One configuration mix-up and we can spend hours looking for the problem. The easiest thing is to first check the configuration of the input and output pins.
- Is the MCLR pin set correctly?
- Is the LVP pin configured correctly for low voltage programming?
Is there any air intake that picks up ambient noise?¶
It must be ensured that all digital inputs and analog inputs are connected to ground by default. Air inlets can cause intermittent failures that are very difficult to find and increased consumption of the circuit. In particular, the MCLR and LPV pins can cause faults that cause the microcontroller to reset if they are not correctly configured and connected to Vdd or GND.
Is the polarity switched?¶
Certain connectors force you to connect a specific polarity. In other cases, a connector has only one position, but the wires are misplaced. A reversed supply can destroy the circuit and a reversed polarity analog signal will not work. Is the polarity correct?
The digital inputs can be active high or low. Is it the correct logic level?
If the reset pin /MCLR is connected with a resistor to ground and a push button to positive (inverse of its normal connection) from there nothing will work. This mistake is not often made and for the same reason a lot of time can be wasted trying to find this bug.
If a microcontroller port is configured with inputs in Pull-up (resistors to positive) and the external pushbuttons are connected to the positive of the supply... none will work.
Is there a short circuit?¶
The multimeter may show continuity in the wires, but if there is a short in between then the circuit will not work. To check the continuity of a component, you must disconnect the component from the circuit. Check that there are no short circuits before checking for continuity.
If this error is frequent, there are specific devices to check for short circuits. From a simple milliohmmeter to a complex time domain reflectometer
Has it been checked for sure?¶
It is worth insisting again on this point. On many occasions it is assumed that there is a connection just because we see the cable in its place. This is not enough. Checking can take very little time and save a lot of effort and some headache.
It is easy to "see" that two red and black terminals are correctly connected to the multimeter... until a closer look reveals that the terminals are connected in voltage mode, while trying to measure current.
Are the variables and constants set correctly?¶
This is a section that generates frequent and difficult to find failures because it is assumed that it should work correctly. The default language that will be discussed is C, because it is the most frequently used high-level language for programming microcontrollers.
Can variables contain data?¶
A loop of 1000 iterations cannot be done with an 8-bit variable. A signed 8-bit variable can only count up to 127. Larger values will confuse the loop by going negative:
signed char i;
for(i=130; i>0; i++) print i;
This code will not print anything, since i is initialized with a value less than zero (i = 130 = -126 in 8-bit signed format).
Is the number base correct? It must be verified that we are using a suitable numbering base:
a = 0x11001100; // Se está utilizando base hexadecimal, aunque parezca binario
Are there repeated definitions or out of context?¶
If there is a definition of a constant "#define" at one point in the program and it is redefined at another point, this can lead to confusion when programming. Definitions must appear only once.
The local variables of a function overlap the global variables. Global variables must be called in such a way that they do not match local variables.
Are the conditions correctly written?¶
Conditions are a frequent source of errors. Some of the most frequent are:
Writing a single equal is incorrect:
if (a = 1) // Incorrecto. Se asigna 1 a la variable 'a'
if (a == 1) // Correcto
if (0xF0 & 0x0F); // Incorrecto, resultado falso
if (0xF0 && 0x0F); // Correcto, resultado verdadero
if ((0xF0 != 0) && (0x0F != 0)); // Más correcto aún, la condición aparece explícita