jcohenadad / espresso-pimpup

DIY project to pimp up my espresso machine to display various measures
MIT License
0 stars 0 forks source link

Timer for brew switch button #2

Open jcohenadad opened 1 month ago

jcohenadad commented 1 month ago

Similar projects:

USING SSR:


Measuring Espresso Shot Duration with Arduino and Rancilio Silvia Machine

Project Overview

This project involves measuring the duration of an espresso shot on a Rancilio Silvia machine using an Arduino. The timer starts when the brew switch is turned on and stops when the switch is turned off.

1. Wiring the Arduino to the Rancilio Silvia Machine

The Rancilio Silvia’s brew switch controls power to the pump or a solenoid valve. We'll use this switch state to detect when the brewing starts and stops.

Components Needed

  1. Optocoupler (Opto-isolator): For isolating high voltage AC and low voltage DC.
  2. Relay Module (Alternative): Safely interfaces high voltage to the Arduino.
  3. Resistors (for current limiting, if needed).
  4. Wires.
  5. Arduino Board.

Wiring Instructions

  1. Identify the Brew Switch Terminals:

    • Open the Rancilio Silvia machine case to access the brew switch.
    • Find the two terminals of the brew switch where it connects to the pump or solenoid valve.
  2. Connect the Optocoupler or Relay to the Brew Switch:

    • Connect the input side of the optocoupler or relay module to the brew switch terminals.
    • When the switch is turned on, it should close the circuit on the input side, allowing the optocoupler or relay to activate.
  3. Connect the Optocoupler or Relay to the Arduino:

    • The output side of the optocoupler or relay should connect to a digital input pin on the Arduino (e.g., Digital Pin 2).
    • Connect the GND of the Arduino to the GND of the optocoupler/relay module.
    • Optionally, connect a pull-down resistor (e.g., 10kΩ) between the digital input pin and GND to ensure the pin reads LOW when the brew switch is off.

2. Arduino Code for Timer Functionality

This code example starts a timer when the brew switch is turned on and stops the timer when the switch is turned off.

// Define the pin connected to the optocoupler/relay output
#define BREW_SWITCH_PIN 2   // Digital pin 2 for brew switch input

// Variables to store timing information
unsigned long startTime = 0;     // Store start time
unsigned long shotDuration = 0;  // Store shot duration

// Variable to keep track of brew switch state
bool isBrewing = false;

void setup() {
  // Initialize the serial communication
  Serial.begin(9600);

  // Set the brew switch pin as input
  pinMode(BREW_SWITCH_PIN, INPUT);

  // Ensure pin starts LOW (brew switch off)
  digitalWrite(BREW_SWITCH_PIN, LOW);

  // Print initial message
  Serial.println("Waiting for brew to start...");
}

void loop() {
  // Read the state of the brew switch
  bool brewSwitchState = digitalRead(BREW_SWITCH_PIN);

  // Check if brew switch has been turned on (HIGH)
  if (brewSwitchState == HIGH && !isBrewing) {
    // Brew just started
    startTime = millis();   // Record the start time
    isBrewing = true;       // Set brewing state to true
    Serial.println("Brew started!");

  } else if (brewSwitchState == LOW && isBrewing) {
    // Brew just stopped
    shotDuration = millis() - startTime;  // Calculate shot duration
    isBrewing = false;                    // Reset brewing state

    // Print the duration in seconds
    Serial.print("Brew stopped! Duration: ");
    Serial.print(shotDuration / 1000.0);  // Convert to seconds
    Serial.println(" seconds");

    // Reset start time for the next shot
    startTime = 0;
  }

  // Add a small delay to debounce the switch
  delay(50);
}

Explanation of the Code

  1. Pin Definition:

    • BREW_SWITCH_PIN: The digital pin connected to the output of the optocoupler or relay module.
  2. Variables:

    • startTime: Records the start time when the brew switch is turned on.
    • shotDuration: Calculates the duration of the shot when the brew switch is turned off.
    • isBrewing: Keeps track of whether the brew is currently ongoing.
  3. Loop Logic:

    • When the brew switch is detected as HIGH (turned on), the timer starts, and startTime is recorded.
    • When the switch is detected as LOW (turned off), the timer stops, and shotDuration is calculated and printed.

3. Testing and Calibration

  1. Upload the code to the Arduino.
  2. Open the Serial Monitor in the Arduino IDE (set baud rate to 9600).
  3. Turn the brew switch on and off and observe the start and stop messages and the shot duration displayed in the Serial Monitor.

4. Safety Considerations

5. Alternative Option - Hall Effect or Reed Switch

If you want a non-invasive approach, you can use a Hall effect sensor or a reed switch to detect when the brew switch is toggled (if the switch uses a magnetic field for actuation). This will eliminate the need to work directly with high-voltage components.

6. Troubleshooting


Let me know if this Markdown version works for you or if you'd like me to include any additional information!

Source: ChatGPT with prompt:

I'd like to count the duration of a shot of espresso using arduino. When the user switches on the brew button on the rancilio silvia machine, the timer starts, and when switch is off, the timer stops. What would be the wiring connection, and arduino code for this?
jcohenadad commented 1 month ago

Problem of SSR: Most SSR are unidirectional, ie: the DC controls the AC. But here i want to do the other way around.

Alternative option: Using current sensor

Sure! Here's the first option written in Markdown format.


Using a Non-Invasive Current Sensor to Detect Brew Switch State

This solution uses a non-invasive current sensor to detect when the brew switch on your Rancilio Silvia espresso machine is turned on or off. This method is safe, simple, and does not require making direct electrical connections to the high-voltage AC circuit.

1. Components Needed

  1. SCT-013-000 Non-Invasive AC Current Sensor

    • Detects AC current through magnetic induction.
    • Available in different current ratings (e.g., 5A, 10A, 30A).
  2. Burden Resistor (if not built-in): 33Ω to 68Ω resistor (needed if the current sensor does not include a built-in burden resistor).

  3. Arduino Board: Any Arduino (e.g., Uno, Nano) to read the current sensor output.

  4. Jumper Wires: For connections between the sensor and the Arduino.

2. Wiring Instructions

  1. Connect the Current Sensor to the AC Wire:

    • Clip the current sensor around the AC wire connected to the brew switch or the pump.
    • The sensor should be clamped around only one wire (either live or neutral, not both).
  2. Wiring the Sensor to the Arduino:

    • Connect the sensor’s output signal wire to an analog input pin on the Arduino (e.g., A0).
    • If your sensor has three wires (VCC, GND, and Signal):
      • VCC5V on the Arduino.
      • GNDGND on the Arduino.
      • SignalAnalog Pin A0 on the Arduino.
  3. Verify Sensor Orientation:

    • Ensure the current sensor is positioned correctly with the arrow indicating the current flow direction.
    • If using a sensor without a built-in burden resistor, connect the burden resistor across the sensor’s output terminals.

3. Arduino Code for Current Sensor

The following code reads the analog signal from the current sensor and determines whether the brew switch is on or off based on a predefined current threshold.

#define CURRENT_SENSOR_PIN A0  // Analog pin to read current sensor value
const int threshold = 50;      // Adjust this threshold based on the sensor's output

bool isBrewing = false;
unsigned long startTime = 0;
unsigned long shotDuration = 0;

void setup() {
  Serial.begin(9600);  // Initialize serial communication
}

void loop() {
  // Read the analog value from the current sensor
  int sensorValue = analogRead(CURRENT_SENSOR_PIN);

  // If current is detected (above threshold), it means the brew switch is on
  if (sensorValue > threshold && !isBrewing) {
    startTime = millis();  // Start timing
    isBrewing = true;
    Serial.println("Brew started!");
  }
  // If current drops below threshold, it means the brew switch is off
  else if (sensorValue <= threshold && isBrewing) {
    shotDuration = millis() - startTime;  // Calculate shot duration
    isBrewing = false;
    Serial.print("Brew stopped! Duration: ");
    Serial.print(shotDuration / 1000.0);  // Convert duration to seconds
    Serial.println(" seconds");
  }

  delay(100);  // Small delay to avoid rapid readings
}

Explanation:

  1. Reading Sensor Values:

    • The code reads the analog value from the current sensor connected to CURRENT_SENSOR_PIN (e.g., A0).
  2. Setting a Current Threshold:

    • The threshold value is used to determine if current is flowing through the wire (indicating the brew switch is on).
    • Adjust this threshold value based on the specific output of your sensor.
  3. Starting and Stopping the Timer:

    • When the current value is above the threshold, it indicates that the brew switch is on, and the timer starts.
    • When the current value drops below the threshold, it indicates that the brew switch is off, and the timer stops.

4. Testing and Calibration

  1. Upload the Code to the Arduino:

    • Use the Arduino IDE to upload the code to your Arduino board.
  2. Open the Serial Monitor:

    • Set the baud rate to 9600 in the Serial Monitor.
    • Observe the start and stop messages along with the shot duration as you toggle the brew switch on and off.
  3. Adjust the Threshold Value:

    • If the sensor does not reliably detect the brew switch state, adjust the threshold value in the code.
    • Increase or decrease the threshold until the sensor correctly detects when the switch is turned on and off.

5. Advantages of Using a Non-Invasive Current Sensor

6. Considerations for Accurate Readings


Additional ressource:

jcohenadad commented 1 month ago

Best solution

Given that I need something to power the arduino, it makes sense to use the AC power line anyway. So one option would be to

Items

Description

Given that you'd like to power the Arduino using the espresso machine’s power supply and detect the activation of the brew switch, we need to focus on a solution that provides:

  1. Safe power conversion to step down the AC voltage to a suitable DC voltage for the Arduino.
  2. Reliable detection of the brew switch activation while ensuring isolation and safety.

Here’s how you can achieve both goals:

1. Powering the Arduino from the Espresso Machine

The Rancilio Silvia operates on a high AC voltage (120V AC or 230V AC depending on your region). To power the Arduino, you need to step down the AC voltage to a safe DC voltage (e.g., 5V or 9V DC).

Recommended Power Supply Options

  1. AC to DC Converter Module (Buck Converter or Power Adapter):

    • Use an AC to DC converter to step down the high AC voltage to a safe DC voltage suitable for the Arduino.
    • Look for a converter that can handle your local AC voltage (e.g., 120V or 230V) and outputs 5V DC (for direct connection to the Arduino) or 9V/12V DC (for the Arduino’s barrel jack).

    Example Modules:

    • AC-DC Converter Module 230V to 5V:
      • Input: 110-230V AC
      • Output: 5V DC, 1A (sufficient for powering an Arduino).
    • LM2596 Buck Converter:
      • Step-down module to convert 12V DC to 5V DC for Arduino.

    Recommended Product:

  2. AC to DC Power Adapter:

    • Use a standard AC-DC adapter plugged into the espresso machine’s power socket.
    • Choose an adapter that provides 9V DC (for the Arduino’s barrel jack) or 5V DC (for direct connection to the Arduino’s 5V pin).

    Recommended Product:

    • 9V 1A Power Adapter (suitable for Arduino Uno and similar boards).
  3. Use the Brew Switch’s Power Directly:

    • If the brew switch directly controls the power to the pump or a heating element, you can tap into this power source to power the Arduino using an AC to DC converter.

Power Wiring:

Safety Considerations:

2. Detecting the Brew Switch Activation

Since you’ll be using the espresso machine’s power to drive the Arduino, we can use a voltage detection solution to monitor the brew switch state. The following two solutions are suitable:

Solution 1: Using an Optocoupler to Detect AC Voltage

This solution is ideal if you want to maintain electrical isolation between the high-voltage AC circuit and the low-voltage Arduino circuit.

Wiring Instructions:

  1. Connect the input side of the optocoupler (LED) in parallel with the brew switch’s AC terminals.
  2. Use a current-limiting resistor (220Ω for 120V AC or 470Ω for 230V AC) in series with the LED of the optocoupler.
  3. Connect the output side of the optocoupler (transistor side) to an Arduino digital input pin (e.g., Digital Pin 2).

Example Wiring:

               Brew Switch (AC Circuit)
            ┌─────────────────────────────┐
            │                             │
            │   AC Live ─────┬───────────┬┘
            │                │           │
            │                │           │
 ┌─────────▼────┐      ┌─────▼────────┐
 │  Optocoupler │      │ AC-DC 5V     │
 │   Input Side │      │ Converter    │
 │    (LED)     │      │ Module       │
 │   ┌───────┐  │      │   AC In      │
 │   │  AC+  │──┘      └───AC In──────┘
 │   │       │
 │   │  AC-  │
 │   └───────┘
 │              ┌───────────────┐
 │              │ Output Side   │
 │              │ (To Arduino)  │
 └─────────────┼───────────────┘
               │ Emitter ─────> GND
               │ Collector ───> Pin 2

Example Code:

#define BREW_SWITCH_PIN 2   // Digital pin connected to the optocoupler's output

bool isBrewing = false;
unsigned long startTime = 0;
unsigned long shotDuration = 0;

void setup() {
  Serial.begin(9600);  // Initialize serial communication
  pinMode(BREW_SWITCH_PIN, INPUT_PULLUP);  // Use internal pull-up resistor
}

void loop() {
  // Read the state of the optocoupler's output
  bool brewSwitchState = digitalRead(BREW_SWITCH_PIN);

  // When the brew switch is on, the optocoupler output will be LOW (inverted logic)
  if (brewSwitchState == LOW && !isBrewing) {
    startTime = millis();  // Start timing
    isBrewing = true;
    Serial.println("Brew started!");

  } else if (brewSwitchState == HIGH && isBrewing) {
    shotDuration = millis() - startTime;  // Calculate duration
    isBrewing = false;
    Serial.print("Brew stopped! Duration: ");
    Serial.print(shotDuration / 1000.0);  // Convert to seconds
    Serial.println(" seconds");
  }

  delay(50);  // Small delay to debounce the signal
}

Solution 2: Using an AC Voltage Sensor Module

You can use an AC voltage sensor like the ZMPT101B AC Voltage Sensor Module to detect voltage changes across the brew switch and send a corresponding signal to the Arduino.

Wiring:

Example Code:

#define VOLTAGE_SENSOR_PIN A0  // Analog pin to read voltage sensor output
const int threshold = 512;     // Midpoint of 0-1023 analog range

bool isBrewing = false;
unsigned long startTime = 0;
unsigned long shotDuration = 0;

void setup() {
  Serial.begin(9600);  // Initialize serial communication
}

void loop() {
  // Read the analog value from the voltage sensor
  int sensorValue = analogRead(VOLTAGE_SENSOR_PIN);

  // If voltage is detected (above threshold), it means the brew switch is on
  if (sensorValue > threshold && !isBrewing) {
    startTime = millis();  // Start timing
    isBrewing = true;
    Serial.println("Brew started!");
  }
  // If voltage drops below threshold, it means the brew switch is off
  else if (sensorValue <= threshold && isBrewing) {
    shotDuration = millis() - startTime;  // Calculate duration
    isBrewing = false;
    Serial.print("Brew stopped! Duration: ");
    Serial.print(shotDuration / 1000.0);  // Convert to seconds
    Serial.println(" seconds");
  }

  delay(100);  // Small delay to avoid rapid readings
}

Choosing the Right Solution

jcohenadad commented 1 month ago

IMG_6701

jcohenadad commented 1 month ago

Little room for optocoupler behind switch... maybe a current probe is better.

jcohenadad commented 1 month ago

Alternatively, I use no additional sensor associated with the switch, and I just rely on the pressure from the pump!!! At a certain threshold, i can start the timer.