Digital Input
Description
Digital inputs are standard input which are available on several modules:
OI-Core/OI-CoreLite (x4)
OI-Discrete (x10)
OI-Mixed (x4)
OI-Stepper (x4)
Main characteristics
The input level is compatible with 3.3V and 5V levels, as well as industrial levels (12V, 24V), up to 30V.
The voltage level on all digital inputs is independent of the power supply. This means you can read the value of a 24V or a 3.3V sensor when the system is supplied with 12V.
A Schmitt trigger comparator with a hysteresis of 1V is used on all inputs to improve immunity to input noise.
An analog input filter with a response time of 45kHz is enabled on all entries to filter noise.
If left floating, the input value will always be ‘0’.
Inputs are compatible with both PNP sensors and push-pull sensors.
Warning
You cannot use NPN sensors with digital inputs.
Characteristics
Requirements |
Value |
Remark |
---|---|---|
Minimum Voltage Input HIGH |
3V |
above this value, a logic ‘1’ is guaranteed |
Maximum Voltage Input LOW |
2V |
below this value, a logic ‘0’ is guaranteed |
Maximum voltage |
30V |
|
Impedance |
4.8kOhms |
|
Sampling frequency |
20kHz |
See note 1 for OI-Core |
Note
Note 1: On OI-Core and OI-CoreLite modules, the maximum input frequency is 1kHz.
Code examples
The example code above demonstrates how to read the value of an input on an OI-Core module.
#include "OpenIndus.h"
#include "Arduino.h"
/* Instanciate */
OICore core;
int din_value[4];
void setup(void)
{
Serial.begin(115200);
}
void loop(void)
{
/* Read value from DIN 1 to 4 */
din_value[0] = core.digitalRead(DIN_1);
din_value[1] = core.digitalRead(DIN_2);
din_value[2] = core.digitalRead(DIN_3);
din_value[3] = core.digitalRead(DIN_4);
/* Print value in monitor */
Serial.printf("DIN_1: %i | DIN_2: %i | DIN_3: %i | DIN_4: %i\n", din_value[0], din_value[1], din_value[2], din_value[3]);
/* Wait for 200ms before reading value again */
delay(200);
}
This one, shows you how to use interrupt on an OI-Discrete module.
#include "OpenIndus.h"
#include "Arduino.h"
/* Instanciate */
OIDiscrete discrete;
/* Callbacks: Print and detach interrupt */
void callback1(void*) { Serial.println("Interrupt on DIN_1 !"); discrete.detachInterrupt(DIN_1); }
void callback2(void*) { Serial.println("Interrupt on DIN_2 !"); discrete.detachInterrupt(DIN_2); }
void callback3(void*) { Serial.println("Interrupt on DIN_3 !"); discrete.detachInterrupt(DIN_3); }
void callback4(void*) { Serial.println("Interrupt on DIN_4 !"); discrete.detachInterrupt(DIN_4); }
void callback5(void*) { Serial.println("Interrupt on DIN_5 !"); discrete.detachInterrupt(DIN_5); }
void callback6(void*) { Serial.println("Interrupt on DIN_6 !"); discrete.detachInterrupt(DIN_6); }
void callback7(void*) { Serial.println("Interrupt on DIN_7 !"); discrete.detachInterrupt(DIN_7); }
void callback8(void*) { Serial.println("Interrupt on DIN_8 !"); discrete.detachInterrupt(DIN_8); }
void callback9(void*) { Serial.println("Interrupt on DIN_9 !"); discrete.detachInterrupt(DIN_9); }
void callback10(void*) { Serial.println("Interrupt on DIN_10 !"); discrete.detachInterrupt(DIN_10); }
void setup(void)
{
/* Attach interrupt*/
discrete.attachInterrupt(DIN_1, callback1, RISING_MODE);
discrete.attachInterrupt(DIN_2, callback2, RISING_MODE);
discrete.attachInterrupt(DIN_3, callback3, RISING_MODE);
discrete.attachInterrupt(DIN_4, callback4, RISING_MODE);
discrete.attachInterrupt(DIN_5, callback5, RISING_MODE);
discrete.attachInterrupt(DIN_6, callback6, RISING_MODE);
discrete.attachInterrupt(DIN_7, callback7, RISING_MODE);
discrete.attachInterrupt(DIN_8, callback8, RISING_MODE);
discrete.attachInterrupt(DIN_9, callback9, RISING_MODE);
discrete.attachInterrupt(DIN_10, callback10, RISING_MODE);
}
void loop(void)
{
delay(1000);
}
Software API
-
class DigitalInputs
Public Static Functions
-
static int digitalRead(DigitalInputNum_t num)
Read an input level. Argument is the DIN to read. The function return an integer that correspond to the value of the DIN.
- Parameters:
num – DIN to monitor.
- Returns:
Value of the DIN input (1 or 0).
-
static void attachInterrupt(DigitalInputNum_t num, IsrCallback_t callback, InterruptMode_t mode, void *arg = NULL)
Attach a user callback to the DIN interrupts.
- Parameters:
num – DIN to attach interrupt.
callback – Handler to execute when an interrupt occurs.
mode – Interrupt mode (RISING, FALLING or CHANGE)
arg – argument for the handler
-
static void detachInterrupt(DigitalInputNum_t num)
Detach interrupt of a given DIN.
- Parameters:
num – DIN to detach interrupt.
-
static int digitalRead(DigitalInputNum_t num)