Example Code for Arduino-Digital Output

Last revision 2025/12/16

This guide offers example code for Arduino digital output control, demonstrating how to connect an LED and turbidity sensor, prepare wiring, and utilize signal switches for effective sensor management.

Hardware Preparation

Connect an LED on pin 13, or use the onboard one; Connect turbidity sensor to Digital Pin 2

Wiring Diagram

Interface Description:

  1. "D/A" Output Signal Switch
    • "A": Analog Signal Output, the output value will decrease when in liquids with a high turbidity
    • "D": Digital Signal Output, high and low levels, which can be adjusted by the threshold potentiometer
  2. Threshold Potentiometer: you can change the trigger condition by adjusting the threshold potentiometer in digital signal mode.

Sample Code

int ledPin = 13;                // Connect an LED on pin 13, or use the onboard one
int sensor_in = 2;                 // Connect turbidity sensor to Digital Pin 2

void setup(){
  pinMode(ledPin, OUTPUT);      // Set ledPin to output mode
  pinMode(sensor_in, INPUT);       //Set the turbidity sensor pin to input mode
}

void loop(){
  if(digitalRead(sensor_in)==LOW){       //read sensor signal
    digitalWrite(ledPin, HIGH);   // if sensor is LOW, then turn on
  }
  else{
    digitalWrite(ledPin, LOW);    // if sensor is HIGH, then turn off the led
  }
}

Result

If sensor is LOW, then turn on the LED; if sensor is HIGH, then turn off the led

Was this article helpful?

TOP