Arduino - digital input
This example demonstrates the use of an M5S digital input module (M5S-BID0524B1) to detect a voltage of 24 V DC. It reads a digital input using the digitalRead function and prints the result to the Serial Monitor.
Since the code uses pinMode(INPUT_PULLUP) instead of pinMode(INPUT), no external pull-up resistor is required. An internal resistor of 20 kΩ in Arduino pulls the input pin to 5 V in passive state. This configuration causes the input to read HIGH when the input module is OFF, and LOW when it is ON.
The circuit
- External: M5S digital input module detecting a voltage of 24 V DC.
- Internal: M5S digital input module connected to Arduino digital pin 2.
Characteristics of different input modules
- M5S-BID0524B1
M5S input Uext -> M5S ON -> Arduino pin voltage 0 V -> Arduino value 0 (LOW)
M5S input passive -> M5S OFF -> Arduino pin voltage 5 V -> Arduino value 1 (HIGH) - M5S-BID0524A1
M5S input GND -> M5S ON -> Arduino pin voltage 0 V -> Arduino value 0 (LOW)
M5S input passive -> M5S OFF -> Arduino pin voltage 5 V -> Arduino value 1 (HIGH)
Code
void setup() {
Serial.begin(9600); // start serial connection
pinMode(2, INPUT_PULLUP); // configure pin 2 as an input and enable the internal pull-up resistor
}
void loop() {
int sensorVal = digitalRead(2); // read the value of pin 2 into a variable
Serial.println(sensorVal); // print the value to the Serial Monitor
}
This example code is based on the Arduino default example:
http://www.arduino.cc/en/Tutorial/InputPullupSerial
Arduino - digital output
This example demonstrates the use of an M5S digital output module (M5S-BOT05750D1b) to switch a load with a maximum voltage of 24 V DC and a maximum current of 750 mA. It writes a HIGH / LOW value to a digital pin using the digitalWrite function and prints the value to the Serial Monitor.
The circuit
- External: M5S digital output module switching a load with a maximum voltage of 24 V DC and a maximum current of 750 mA.
- Internal: M5S digital output module connected to Arduino digital pin 3.
Characteristics of different output modules
- M5S-BOT05750D1b
Arduino value 0 (LOW) -> Arduino pin voltage 0 V -> M5S ON -> M5S output Uext
Arduino value 1 (HIGH) -> Arduino pin voltage 5 V -> M5S OFF -> M5S output passive - M5S-BOT05750C1b
Arduino value 0 (LOW) -> Arduino pin voltage 0 V -> M5S ON -> M5S output GND
Arduino value 1 (HIGH) -> Arduino pin voltage 5 V -> M5S OFF -> M5S output passive
Code
void setup() {
Serial.begin(9600); // start serial connection
pinMode(3, OUTPUT); // configure pin 3 as an output
}
void loop() {
writeAndPrint(HIGH);
delay(1000); // wait 1 second
writeAndPrint(LOW);
delay(3000); // wait 3 seonds
}
void writeAndPrint(int value) {
digitalWrite(3, value); // write the value to pin 3
Serial.println(value); // print the value to the Serial Monitor
}
This example code is based on the Arduino standard sample:
http://www.arduino.cc/en/Tutorial/InputPullupSerial
Arduino - analogue input
This example demonstrates the use of M5S analogue input modules to read 0-20 mA / 0-10 V signals. It uses the analogRead function to read an analogue input pin, which results in a value between 0 and 1023. Also prints the value to the Serial Monitor.
The circuit
- External: M5S analogue input module connected to a 0-20 mA / 0-10 V signal.
- Internal: M5S analogue input module connected to Arduino analogue pin A4.
Characteristics of different output modules
- M5S-AIA05020B3
M5S input 0 mA -> Arduino pin voltage 0 V -> Arduino value 0
M5S input 20 mA -> Arduino pin voltage 5 V -> Arduino value 1023 - M5S-AIV05010B3
M5S input 0 V -> Arduino pin voltage 0 V -> Arduino value 0
M5S input 10 V -> Arduino pin voltage 5 V -> Arduino value 1023
Code
const int analogInPin = A4; // analogue input pin
void setup() {
Serial.begin(9600); // start serial connection
}
void loop() {
int sensorValue = analogRead(analogInPin); // read the value of analogInPin into a variable
Serial.println(sensorValue); // print the value to the Serial Monitor
// wait 2 milliseconds before the next loop for the analogue-to-digital
// converter to settle after the last reading
delay(2);
}
This example code is based on the Arduino standard sample:
http://www.arduino.cc/en/Tutorial/AnalogInOutSerial
Arduino - analogue output
This example demonstrates the use of M5S analogue output modules to output 0-20 mA / 0-10 V signals. It uses the analogWrite function to set the pulse width modulation (PWM) of a digital output pin. The value has to be between 0 and 255. Also prints the value to the Serial Monitor.
The circuit
- External: M5S analogue output module outputs 0-10 mA / 0-10 V signal.
- Internal: M5S analogue output module connected to Arduino digital pin 5.
Characteristics of different output modules
- M5S-AOA05020D3Ab
Arduino value 0 -> Arduino pin voltage always 0 V -> M5S ON -> M5S output 20 mA
Arduino value 255 -> Arduino pin voltage always 5 V -> M5S OFF -> M5S output 0 mA - M5S-AOV05010D3Ab
Arduino value 0 -> Arduino pin voltage always 0 V -> M5S ON -> M5S output 10 V
Arduino value 255 -> Arduino pin voltage always 5 V -> M5S OFF -> M5S output 0 V
Code
const int analogOutPin = 5; // analogue output pin
void setup() {
Serial.begin(9600); // start serial connection
}
void loop() {
writeAndPrint(0);
delay(1000); // wait 1 second
writeAndPrint(127);
delay(1000); // wait 1 second
writeAndPrint(255);
delay(3000); // wait 3 seconds
}
void writeAndPrint(int value) {
analogWrite(analogOutPin, value); // change the value of analogOutPin
Serial.println(value); // print the value to the Serial Monitor
}
This example code is based on the Arduino standard sample:
http://www.arduino.cc/en/Tutorial/AnalogInOutSerial