9. Input Devices¶
group assignment: • probe an input device’s analog levels and digital signals
individual assignment: • measure something: add a sensor to a microcontroller board that you have designed and read it
I tested the following sensors: Push Button Turbidity sensor
Turbidity Sensor

Measured water quality using turbidity sensor in preparation to my final project. Turbidity sensor measures the cloudiness of a liquid caused by suspended particles. These particles scatter the incident light.
How Turbidity sensor works Turbidity sensors measure the amount of light that is scattered by suspended solids in a liquid, such as water. When the concentration of total suspended solids (TSS) and total dissolved solids (TDS) in a liquid increase, the turbidity also increases.
When measuring turbidity, most often, you will see the units NTU or JTU.
NTU = Nephelometric Turbidity Units JTU = Jackson Turbidity Units
I worked with turbidity and whenever it detects particles in water the led blinks and it prints on the serial.
I used chatgpt to generate the code, you can replicate the code
// Pin configuration
const int buttonPin = 0; // D0
const int ledPin = 10; // D10
// Button state tracking
int buttonState;
int lastButtonState = HIGH;
// Timing variables
unsigned long lastPressTime = 0;
unsigned long buttonDownTime = 0;
// Press logic
int pressCount = 0;
// Timing thresholds (in milliseconds)
const unsigned long doubleClickInterval = 400;
const unsigned long longPressTime = 1000;
// LED state
bool ledOn = false;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
// Read button state
buttonState = digitalRead(buttonPin);
// Detect button press (HIGH → LOW)
if (buttonState == LOW && lastButtonState == HIGH) {
buttonDownTime = millis();
}
// Detect button release (LOW → HIGH)
if (buttonState == HIGH && lastButtonState == LOW) {
unsigned long pressDuration = millis() - buttonDownTime;
// LONG PRESS → Turn LED OFF
if (pressDuration >= longPressTime) {
ledOn = false;
digitalWrite(ledPin, LOW);
Serial.println("Long press → LED OFF");
pressCount = 0;
}
// SHORT PRESS handling
else {
unsigned long now = millis();
// Check for double click
if (now - lastPressTime < doubleClickInterval) {
pressCount++;
if (pressCount == 2) {
ledOn = true;
digitalWrite(ledPin, HIGH);
Serial.println("Double click → LED ON");
pressCount = 0;
}
} else {
pressCount = 1;
}
lastPressTime = now;
}
}
// Update last state
lastButtonState = buttonState;
}
Push button I used push button 12mm as my input and led as output and connected this to my analog pin D0, when i press the button twice quickly the led turns on and if i hold it for 1 second it tuens off i
// Pin configuration
const int buttonPin = 0; // D0
const int ledPin = 10; // D10
// LED state
bool ledOn = false;
// Timing variables
unsigned long buttonPressTime = 0;
unsigned long lastReleaseTime = 0;
// Timing thresholds (in milliseconds)
const unsigned long doubleClickTime = 400;
const unsigned long longPressTime = 1000;
// Click tracking
int clickCount = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
// Track button state
static bool lastState = HIGH;
bool currentState = digitalRead(buttonPin);
// Detect button press (HIGH → LOW)
if (lastState == HIGH && currentState == LOW) {
buttonPressTime = millis();
}
// Detect button release (LOW → HIGH)
if (lastState == LOW && currentState == HIGH) {
unsigned long pressDuration = millis() - buttonPressTime;
// LONG PRESS → Turn LED OFF
if (pressDuration > longPressTime) {
ledOn = false;
digitalWrite(ledPin, LOW);
Serial.println("LONG PRESS → LED OFF");
clickCount = 0;
}
// SHORT PRESS handling
else {
unsigned long now = millis();
// Check for double click
if (now - lastReleaseTime < doubleClickTime) {
clickCount++;
if (clickCount == 2) {
ledOn = true;
digitalWrite(ledPin, HIGH);
Serial.println("DOUBLE CLICK → LED ON");
clickCount = 0;
}
}
else {
clickCount = 1;
}
lastReleaseTime = now;
}
}
// Update last state
lastState = currentState;
}