IMDHMIControls LibraryShop main store

Troubleshooting

CODESYS analog inputs read 0 on an I2C ADC: where the chain breaks

Controllers and I/O Published 7 min read By IMDHMI controls team

Short answer

Relays that switch and an i2cdetect entry at 16#68 prove the I2C bus is sound, and raw bytes that track the input prove the MCP3422 converts. The zero therefore sits in the UniPi_AI driver layer or the I/O mapping. Poll the chip with a raw read to confirm the split, then fix the mapping, the bus-cycle task and the Always update variables flag instead of the bus.

On this page
  1. Why the fixes you already tried cannot reach the fault
  2. Trace the value stage by stage
  3. Prove the ADC and the bus with a raw read
  4. Timing a 4-byte poll against a 3.75 SPS conversion
  5. Fix the mapping before you blame the driver
  6. Read the chip yourself
  7. What each symptom decides
  8. Questions asked

Working relays and an i2cdetect entry at address 16#68 prove the I2C bus on this Raspberry Pi is enabled, clocked and electrically sound. A raw read with I2C_master.read(16#68, ADR(baReadBufferV), 4) that returns bytes tracking the input voltage proves the MCP3422 converts. When both of those pass and the analog channels still read 0 in CODESYS, the zero sits above the bus: in the UniPi_AI device driver layer or in the I/O mapping that feeds AI1 and AI2. Editing the boot configuration, reinstalling the runtime and re-downloading the application all work below or beside that layer, so none of them can fix it. Poll the chip yourself to confirm the split, then repair the mapping.

Why the fixes you already tried cannot reach the fault #

Every fix attempted so far works on the transport layer, and the transport layer already passes its tests. The onboard relays switch over the same I2C bus, and i2cdetect lists the chip at 16#68; between them, those two results show the bus is enabled, clocked and electrically sound.

  • Boot configuration entries that enable the I2C interface and set its clock to 400000 bit/s work on the wire and its speed. They have no effect on what a CODESYS device driver does with the bytes it receives.
  • The runtime setup script reinstalls the runtime and its dependencies. It does not change how the UniPi_AI device object configures the ADC or maps its channels.
  • Clean all and download rebuilds the application. A driver that returns 0 still returns 0 after a rebuild.
  • Calling AI1 and AI2 from a program proves the code reads the variables. It does not prove the driver wrote to them.

Trace the value stage by stage #

The analog value crosses five stages between the field device and your engineering units. Each stage fails with a characteristic value, and each characteristic value points at one stage:

  1. Terminal voltage at the analog input: a meter showing 0 V at the terminal means a wiring or supply fault, and nothing downstream matters yet.
  2. Front-end divider: the board scales the terminal voltage down into the ADC's input range by a fixed ratio given in the board documentation.
  3. MCP3422 conversion: the chip converts and holds the result plus a configuration and status byte in its output register.
  4. Driver decode: the UniPi_AI driver reads that register, decodes it and writes the mapped channel.
  5. Application scaling: your program turns the channel value into engineering units.

A raw channel stuck at 0 with a live bus always means stage 4 or stage 5.

Prove the ADC and the bus with a raw read #

  1. Run i2cdetect on the Pi. The entry at 16#68 must show the plain address; when it shows UU, a kernel driver has claimed the chip and userspace access will conflict with it. The page on what an i2cdetect UU entry means and the wiring behind it walks that case.
  2. Apply a known, stable voltage to AI1 and confirm it with a meter at the terminal, inside the input range the board documentation allows; the front end has no overvoltage headroom to spare.
  3. In CODESYS, read four bytes from the chip with the library's read method, for example I2C_master.read(16#68, ADR(baReadBufferV), 4).
  4. Step the input voltage and watch the buffer. Data bytes that follow the voltage clear the ADC and the bus.
  5. Check the last byte returned: that is the configuration and status byte. If the ready flag never changes, no conversions are running; if the channel bits do not match the channel you asked for, the wrong channel is selected.

When the data bytes track the input, the ADC and the bus are healthy and the zero is being written above them.

Timing a 4-byte poll against a 3.75 SPS conversion #

The inputs for this case come from the setup itself: the bus clock is 400000 bit/s, set in the boot configuration on this Pi; the read length is 4 bytes, the reply at the highest resolution, three data bytes plus the configuration byte, as used in the raw-read call above; the conversion rate is 3.75 samples per second, the slowest of the four rates the Microchip MCP3422 documentation lists (3.75, 15, 60 and 240 samples per second).

The rule, in words: time per conversion equals one divided by the rate, and time per read equals the bits on the wire divided by the bus clock, with I2C carrying 9 bits per byte, eight data bits plus the acknowledge.

The arithmetic:

  • One conversion: 1 / 3.75 samples per second = 0.2667 s, about 267 ms.
  • One read: 5 bytes on the wire (device address byte plus 4 data bytes) x 9 bits = 45 bits; 45 / 400000 bit/s = 0.000113 s, about 0.11 ms.
  • Ratio: 267 ms / 0.11 ms is roughly 2400.

The result: a read is about three orders of magnitude shorter than one conversion, so bus speed can never be the bottleneck for this chip.

The decision: at the slow documented rate a fresh value arrives every 267 ms, so if the mapped channel still reads 0 for more than two conversion periods, more than about 533 ms here or about 8.3 ms at the 240 samples per second setting, while the raw bytes follow the input, timing is excluded. The fault is the driver or the mapping, not the chip and not the wire.

Fix the mapping before you blame the driver #

CODESYS writes a mapped variable only when it believes something consumes it and a task drives the device. Work through the mapping in this order:

  1. Open the I/O mapping of the UniPi_AI device and confirm AI1 and AI2 are mapped to the channels your program reads.
  2. Confirm the device is assigned to a bus-cycle task that actually runs in the task configuration.
  3. Enable Always update variables on the mapped channels, so the runtime writes them even where no program currently references them.
  4. Log in and check the device log and the driver's diagnostic output for errors.

A mapping that passes all four while the raw bytes still track the input leaves exactly one suspect: the driver is not producing values on this hardware.

Read the chip yourself #

If the mapping is right and the channel stays at 0, stop tuning the bus and drive the MCP3422 from your own code. Remove the UniPi_AI device from the project or leave it unused first: two masters writing different configuration bytes to the same chip overwrite each other's channel selection.

  1. Write the configuration byte for the channel, conversion mode, rate and gain you want. The register is 8 bits wide and selects the input channel, conversion mode, conversion rate and PGA gain; take the bit positions from the configuration-register table in the Microchip MCP3422 datasheet. Only the read call is evidenced in this case, so check the write method signature in your I2C library before you call it.
  2. Poll the 4-byte read until the ready flag shows a new result and the channel bits match the channel you requested.
  3. Assemble the three data bytes for the highest resolution, sign-extend them, and scale with V_adc = raw x (2 x VREF / 2^N) / PGA, where N is the selected resolution and VREF the reference voltage from the datasheet.
  4. Multiply by the front-end divider ratio from the board documentation to get terminal volts.

Turning a code into volts is the same counts-to-engineering-units job as any wired input card, and the article on why an S7-300 analog input reads 0 to 27648 counts works the same arithmetic. Read the trend before blaming the conversion too: a value proportional to the input but off by a constant factor is a scaling problem, a value that jumps or wraps as the input rises is a byte-assembly or sign-extension problem, and a value that follows the wrong input is a channel-select problem.

What each symptom decides #

  • 0 V at the terminal: field wiring or supply. Fix it in the cabinet before touching software.
  • Frozen or all-ones raw bytes: the chip is not converting or not being addressed. Check the configuration byte and the addressing.
  • Raw bytes vary but the mapped channel stays at 0: driver or I/O mapping. Fix the mapping, the bus-cycle task and Always update variables first, then drive the chip yourself.
  • Scaled value proportional to the input but off by a fixed factor: decode, gain or divider scaling, a software scale to correct. The same family of mismatch as a millivolt sensor on a 0-10 V input, which reads zero for a range reason instead.
  • Value jumps or wraps as the input rises: byte assembly or sign extension in the decode.

In this installation the raw bytes vary and the channels read 0, so the decision is the third one: repair the UniPi_AI mapping and task assignment, and if the channel stays at 0 after that, read the MCP3422 directly and scale its codes yourself.

Questions asked

Why does a generic MCP3424 device return data when the board's own driver returns 0?

The MCP3424 is the four-channel sibling of the MCP3422 and shares its register structure, so a generic device descriptor talks to the chip correctly and produces data. It knows nothing about this board's front-end divider, so every value is off by a fixed ratio. It assumes four channels, so the indices may not line up, and its default resolution and gain may not match its own scaling. Values proportional to the input but off by a constant factor confirm exactly this.

How do I tell from the configuration byte that conversions are running?

Read four bytes and look at the last one. The ready flag changes state each time a new conversion result lands; if it never toggles, the chip is not converting, so check the conversion-mode bit and that a conversion has been started. If the ready flag toggles but the channel bits do not match the channel you selected, the chip is converting the wrong input and the reading belongs to another channel.

Can my own code and the board's analog driver share the chip at once?

No. The driver and your code each write their own configuration byte to the same chip, and whichever writes last decides the channel, mode, rate and gain. Remove the board's analog device from the project or leave it unmapped while you poll the chip yourself, and discard the first reading after every channel change until the channel bits in the returned byte match the channel you requested.

What controls the update rate of the MCP3422?

A field in the 8-bit configuration register. The documented settings are 3.75, 15, 60 and 240 samples per second; on this family the faster settings run at lower resolution, so pick the pair that fits your loop. At 3.75 samples per second one conversion takes about 267 ms, which sets how long you must wait for a fresh value.

Keep reading

  • Troubleshooting

    Why a CODESYS analog input stays at zero on one channel

    A CODESYS analog input stuck at zero on one channel while the other channels respond is a channel fault, not a scaling problem: no formula recovers a value that never arrives. Measure the voltage at the channel's terminals and reference, watch the mapped input word online with the scaling untouched, then move the same signal to a second channel to prove which part is dead.

    Controllers and I/O

  • Troubleshooting

    Detect a failed analog input channel and report it to SCADA

    Read the raw input word and the module's per-channel diagnostics before you touch the SCP scaling, then alarm channel health as its own tag that names the channel number. A failed channel then reaches SCADA as a fault with an identity, not as a plausible scaled level, so you know whether to carry a card, a transducer, or wire.

    Controllers and I/O

  • Troubleshooting

    Why an SM 1231 AI8 reads 32767 on unwired channels

    On an S7-1200, an SM 1231 AI8 with only its 24 V supply connected reports the overflow value on every unconnected channel because a floating input has no defined signal. Short unused voltage channels, set unused current channels to the 0 to 20 mA range with wire-break reporting off, and download the hardware configuration.

    Controllers and I/O