Bottle filling with capacitive and photoelectric sensors
Detect a bottle with a photoelectric sensor and its liquid level with a capacitive sensor, run a pump through an SSR until it is full, and count the bottles.
Industrial · Intermediate
In a real product: Bottling line filler (Industrial): Sensors detect each bottle and its liquid level and stop the pump when it is full.
Factories detect things without touching them. A photoelectric sensor sees an object by the light it reflects, and a capacitive sensor feels material, even liquid through a plastic wall. Together they are a filling station: is there a bottle, and is it full?
What you'll learn
- Wiring 3-wire sensors: brown +24 V, blue 0 V, black the switched output
- What a PNP output does: it sources +24 V when active, and floats otherwise
- Choosing the right sensor for the job: diffuse photoelectric for objects, capacitive for level
- A filling cycle as logic: pump on while a bottle is present and not yet full
How it works
Both sensors are powered from the 24 V supply. Their black outputs go to optocoupler inputs IN1 (capacitive: full) and IN2 (photoelectric: bottle present), so the 5 V pins see them as active LOW.
When a bottle arrives (click the photoelectric sensor) the pump starts through a solid-state relay, and the green lamp shows it is running. When the liquid reaches the capacitive sensor (click it) the pump stops and the bottle counts as filled. Removing the bottle, then the liquid, arms the next cycle.
Try this
- Change the capacitive sensor's contact to NC: its logic inverts, a "dark-on" style that also reports a broken wire.
- Give the pump a 10 s timeout and a fault message if the level is never reached.
- Count bottles per minute and print the rate.
sketch.ino
// A filling station: pump while a bottle is present (photoelectric) and not full (capacitive).
const int FULL = 2; // optocoupler OUT1: LOW while the capacitive sensor sees liquid
const int BOTTLE = 3; // optocoupler OUT2: LOW while the photoelectric sensor sees a bottle
const int PUMP = 8; // SSR input
int filled = 0;
bool pumping = false;
void setup() {
Serial.begin(9600);
pinMode(FULL, INPUT);
pinMode(BOTTLE, INPUT);
pinMode(PUMP, OUTPUT);
Serial.println("Waiting for a bottle");
}
void loop() {
bool full = digitalRead(FULL) == LOW;
bool present = digitalRead(BOTTLE) == LOW;
bool shouldPump = present && !full;
if (shouldPump != pumping) {
pumping = shouldPump;
digitalWrite(PUMP, pumping ? HIGH : LOW);
if (pumping) {
Serial.println("Filling");
} else if (present && full) {
filled++;
Serial.print("Full. Bottles filled: ");
Serial.println(filled);
}
}
}