Ir al contenido

Reading digital and analog inputs, driving digital and analog outputs

With the pin map from the previous lesson, you can now read a real sensor and drive a real output. This lesson covers the four basic I/O calls you will use in almost every sketch: digitalRead, digitalWrite, analogRead and analogWrite, and shows how to see the values on your computer.

What you will learn

  • Read a digital input and drive a digital output.
  • Print values to the Serial Monitor to see what the PLC reads.
  • Read an analog input and scale the raw value to a physical unit.
  • Wire inputs safely, knowing industrial inputs are not 5 V logic.

Before you wire: industrial voltage levels

If you come from a bare Arduino, this is the biggest difference. Industrial inputs and outputs work at the voltage levels in your model's datasheet, typically 12 to 24 V, not at 5 V logic. Check the datasheet for the allowed range of every terminal and power everything off before wiring. Applying the wrong voltage to a terminal can damage the board.

See what the PLC reads: the Serial Monitor

Your sketch can send text to the computer over the same USB cable you use for uploading. Call Serial.begin(9600) in setup, print with Serial.println, and open Tools > Serial Monitor at the same speed (9600 baud).

Reading a digital input

A digital input reads one of two states, high or low, typically wired to a switch, a proximity sensor or a relay contact. Declare the pin as INPUT in setup, then read it in loop.

void setup() {
  Serial.begin(9600);
  pinMode(I0_0, INPUT);
}

void loop() {
  bool state = digitalRead(I0_0);
  Serial.println(state);
  delay(200);
}

Apply and remove the signal on I0_0 and watch the value change between 0 and 1 in the Serial Monitor.

Driving a digital output

A digital output switches between high and low, typically driving a relay, an indicator, or a control signal to another device. You already used this call in the first upload lesson.

void setup() {
  pinMode(Q0_0, OUTPUT);
}

void loop() {
  digitalWrite(Q0_0, HIGH);
}

Putting both together: an input controls an output

This is the smallest useful PLC program: output Q0_0 follows input I0_0. Wire a switch to I0_0 and watch the output indicator.

void setup() {
  pinMode(I0_0, INPUT);
  pinMode(Q0_0, OUTPUT);
}

void loop() {
  digitalWrite(Q0_0, digitalRead(I0_0));
}

Reading an analog input

An analog input reads a continuous value instead of just high or low, typically wired to a 0 to 10 V or 4 to 20 mA sensor depending on your board variant. analogRead returns a raw integer that you then scale to the real physical range of your sensor.

void setup() {
  Serial.begin(9600);
}

void loop() {
  int rawValue = analogRead(I0_6);
  Serial.println(rawValue);
  delay(500);
}

The raw value alone is not a physical measurement. Check the datasheet of your board for the resolution and input range, and do not assume a fixed scale across all variants.

Scaling the raw value

The scaling is linear. Suppose, as an example, your board returns 0 to 1023 for a 0 to 10 V input and your sensor outputs 0 to 10 V for 0 to 100 percent of its range:

float volts = rawValue * 10.0 / 1023.0;
float percent = volts * 10.0;

Replace the constants with the real resolution and range of your model and sensor. The same pattern, raw to volts to physical unit, applies to every analog sensor.

Driving an analog output

Not every board variant exposes analog outputs. Where available, analogWrite drives a variable signal instead of a simple on or off state, useful for controlling a valve position or a variable speed drive reference signal.

void setup() {
}

void loop() {
  analogWrite(A0_0, 128);
}

Check your datasheet for the value range that corresponds to the full-scale output of your model.

Next lesson: wired communication, how the PLC talks to other devices. Take the quiz below first to check what you have learned.

Calificación
0 0

No hay comentarios por ahora.

para ser el primero en comentar.