Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 37 ยท Automation Basics

Digital and Analog I/O in PLCs

Understand PLC digital and analog inputs and outputs, 0-10 V and 4-20 mA signals, raw counts, scaling to engineering units, filtering and wire-break detection.

PLC inputs and outputs (I/O) come in two families. Digital (discrete) I/O has two states: on or off. Analog I/O represents a range of values, such as a temperature from 0 to 150 degrees or a valve position from 0 to 100 percent. Most of the work in a real program is turning raw signals into meaningful numbers and back.

This lesson shows how each type works, how a 4-20 mA signal becomes a number in your program, and how to scale, filter and protect an analog input.

Digital I/O

A digital input sees a voltage (usually 24 V DC) and reports a BOOL. A digital output switches a load on or off. Typical devices:

Digital inputs Digital outputs
Push buttons, selector switches Contactor coils, relays
Limit switches, proximity sensors Indicator lamps, horns
Photo-eyes, float switches Solenoid valves

Input modules usually have filter time (a few milliseconds) that ignores contact bounce: a mechanical switch flickers for a moment when it closes. It also means a very short pulse may be ignored.

C Stand-in: debouncing a noisy switch (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Analog signals

Analog sensors (pressure, level, flow, temperature) send a smoothly varying electrical signal. The two most common standards:

  • 0-10 V: easy to wire and read, but sensitive to wiring noise and voltage drop over distance.
  • 4-20 mA: a current loop. It resists noise, works over long cables, and has a built-in fault detector: 4 mA means "zero", 20 mA means "full", and 0 mA means a broken wire.

4-20 mA signal

4 mA Bottom of range
20 mA Top of range
Below 3.6 mA Probably a broken wire

Live zero makes faults visible

1 / 2

The analog input module converts the signal into a raw count with an ADC (analog-to-digital converter). A 16-bit signed module might return 0 to 27 648 for the nominal range (a Siemens convention); others use 0-32 767 or 0-65 535. Always read the module documentation.

flowchart LR
    T["Sensor<br/>4-20 mA"] --> M["Analog module<br/>ADC"]
    M --> R["Raw count<br/>0 to 27648"]
    R --> S["Scaling<br/>in your program"]
    S --> E["Engineering value<br/>0 to 150 C"]

Figure: from field signal to engineering units.

Scaling

Scaling is a straight line: map the raw range to the real range.

value = out_min + (raw - raw_min) * (out_max - out_min) / (raw_max - raw_min)

C Stand-in: scale a raw count to degrees (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

For 4-20 mA the module may report 4 mA as raw 0 (when set to 4-20 mA range), or the program may need to subtract the offset itself. Check the datasheet, and test with a signal simulator.

Filtering

Analog readings jitter due to noise. A cheap smoothing method is a first-order filter (exponential average): filtered = filtered + k * (raw - filtered), with k between 0 and 1. Smaller k means smoother but slower.

C Stand-in: filtering a noisy reading (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Wire-break and range checks

A good analog routine checks for faults before scaling:

  • Raw below the allowed minimum (e.g. under 3.6 mA) means a broken wire or a dead sensor.
  • Raw above the maximum (e.g. over 21 mA) means a short or an over-range.

Then it freezes or substitutes a safe value and raises an alarm, instead of feeding garbage to the control logic.

C Stand-in: range check before using a value (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Analog outputs

An analog output sends a value to drive a variable-speed drive or a control valve. You scale in the opposite direction: engineering units (0-100 % open) to a raw count the module sends out as 4-20 mA or 0-10 V. Limit the value to the allowed range first.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is 4-20 mA often preferred over 0-10 V in factories?

    Current loops are less affected by noise and voltage drop, and a 0 mA reading shows a fault.
  2. 2. What does a 4-20 mA sensor reading of 0 mA most likely mean?

    Because the live zero is 4 mA, 0 mA means the loop is open.
  3. 3. What does scaling do?

    Scaling converts raw counts to values like degrees or percent.
  4. 4. A smaller filter constant k makes the filtered value:

    With small k each new sample changes the result only a little, smoothing noise but delaying response.

Summary

  • Digital I/O is on/off, with input filtering for contact bounce.
  • Analog I/O uses 0-10 V or 4-20 mA and raw counts from an ADC.
  • Scale raw values linearly, filter noise and check for wire breaks.
  • Analog outputs reverse the scaling and must be limited.

Next: Sensors and Actuators