askDrAlex
Posts: 11
Joined: Sat Dec 21, 2019 12:23 am

WashbearyPi - Controlling a washing machine with a Pi

Sat Dec 21, 2019 10:15 pm

Hi!

This is the beginning of a long project, that will keep me busy for at least a few months, maybe even years. Here, I will report on the progress of my project which is about controlling a washing machine with a RaspberryPi.

The name of this project will be "WashbearyPi". An in-depth explanation of this name will be given later, when I will also present the project logo. For the moment, I just want to emphasize that the spelling is indeed intended to be "Washbeary" and not "Washberry", so this is not a typo.

As many of you probably know, modern washing machines are controlled by microcontrollers. Inside a washing machine, there is a number of actuators, like:

- the motor that drives the drum
- the electric heater
- the solenoid valve that controls the water inlet
- the drain pump
- the magnet that locks the door

Also, there are some sensors, like:

- the water temperature sensor
- the water level sensor
- the motor speed sensor
- the door switch (opened/closed)

Modern machines may have much more of them, e.g. acceleration sensors to detect mass imbalance inside the drum when it is spinning.

The final goal of this project is to remove the manufacturer`s control electronics out of the machine and get all sensors and actuators under the control of the Pi, such that individual wash programs can be designed and run on the machine (Just imagine - you'll be able to E-Mail your latest 60-degree cotton program to all your friends, to run on their machines as well :-) ). For this purpose, I will use the GPIO pins of the Pi. Apparently, appropriate interface circuits will have to be designed. For reading temperature values from the NTC temperature sensor, for example, an ADC will be required.

Today, I am starting with sub-topic 1: THE ELECTRIC MOTOR

First, I should say that different motor designs on washing machines exist:
1. The classical design is an AC motor with brushes, that drives the drum via a belt.
2. Some machines have brushless motors which are controlled via frequency inverters and also drive the drum via a belt.
3. Some more recent machine designs use direct-drive inverter motors which are directly plugged onto the shaft of the drum, not having a belt any more.

The machine that will serve as my test object (production year: 2004, with its control electronics broken) has the classical motor design (1).
The motor has a 7-pole connector, whose pinout I reverese engineered to be as follows:
pinout.jpg
pinout.jpg (42.3 KiB) Viewed 1668 times
Pin 1+2: Inductive speed sensor
Pin 3+4: Motor brushes (i.e. rotor coil)
Pin 5+6+7: Stator coils (a long and a short one, connected in series)

Besides by resistance measurement, I did the distinction of long and short stator coil by an induction measurement: I fed a constant current through the coil under test, rotated the motor by hand at a defined speed, and measured the voltage induced in the rotor coil. Absolute value and sign of this voltage, both together, told me how the stator is wired internally.

In the next step, I connected the rotor coil and both stator coils in series and energized them with a DC lab power supply. It turns out that something like 10 V is enough the get the motor into motion (although it is designed for 230V AC). At 30V (the maximum my power supply can provide), the (empty) drum spins at roughly 100 RPM.
The motor speed sensor delivers a sinusoidal voltage (see the oscilloscope screenshot below) whose frequency and amplitude increase with increasing motor speed.
motor_on_DC.jpg
motor_on_DC.jpg (140.48 KiB) Viewed 1668 times

[Yes, I know, this oscilloscope is not quite up-to-date. To my knowledge, its manufacturing date was roughly between the Precambrian and the Stone Age :-)]

Next, I need to think about how to run the motor on 230V AC while controlling its speed with the RaspberryPi. For sure, a triac will be involved...

askDrAlex
Posts: 11
Joined: Sat Dec 21, 2019 12:23 am

Re: WashbearyPi - Controlling a washing machine with a Pi

Mon Dec 23, 2019 9:34 pm

Since moving pictures are known to be more illustrative than static ones, I uploaded a video showing the motor in motion:

https://www.youtube.com/watch?v=WtgMESLjLoQ

askDrAlex
Posts: 11
Joined: Sat Dec 21, 2019 12:23 am

Re: WashbearyPi - Controlling a washing machine with a Pi

Thu Dec 26, 2019 11:23 pm

In this project, there will be a number of actuators that run on 230V AC and need to be controlled by the Pi. For some of them (e.g. water inlet solenoid valve, drain pump etc.), the switching state is just binary: OFF or ON. In that case, a simple relay could be used. However, since the electronics in a washing machine will be exposed to mechanical vibrations, it is desirable to keep the number of moving parts as low as possible. For low-power actuators, I will therefore use semiconductor switches, like most washing machine manufacturers do.

I chose a BT139-800E triac as the front-end switching device. Its gate is controlled via an optocoupler with triac output stage, the MOC3081. The latter provides galvanic isolation between the "hot" side (grid voltage) and the Raspberry's world of 3.3V and 5V. According to the data sheet, the MOC3081 requires a transmitter diode (LED) current of 15 mA to guarantee that its output triac will be gated. The absolute maximum rating for the transmitter diode current is 60 mA. So wee need more than 15, but less than 60 mA of current.
I read through the Pi's specifications and found that a single GPIO pin can deliver up to 16 mA of current, however, all GPIO pins in sum shall not deliver more than 51 mA. Accordingly, driving a single MOC3081 directly would be within the capabilities of the Pi. However, if more of them are meant to be used (like I will do for my project), the current limit of the GPIOs will be hit. Ideally, the GPIO current should therefore be kept below 3 mA for each pin, to make sure the 51 mA sum limit cannot be exceeded.
For this reason, I use a BC548 transistor to drive the transmitter diode of the MOC3081. A 10kOhm resistor between the GPIO pin and the transistor's base limits the current to less than 0.4mA, which is far below the 3 mA limit.

For my first tests, I used the following circuit:
triac1.png
triac1.png (9.7 KiB) Viewed 1570 times
That is how it looks in real life:
triac_circuit1.jpg
triac_circuit1.jpg (111.22 KiB) Viewed 1570 times
The purpose of R4 and C1 is to limit the voltage rise rate dU/dt between the main terminals of the triac. Most triacs do not like a too rapid voltage rise, as this might ignite them even in the absence of a gate current. For purely resistive loads (like a light bulb, which was my first test load), it is not necessary to have R4 and C1. The circuit works with and without. On larger inductive loads (motor), I did not try without, as my concern was to damage the triac.

Controlling the washing machine motor will be somewhat more challenging, because it is not just an issue of turning it ON or OFF, but it needs continuous speed control. I guess a phase cutting approach should be a good choice. The BT139 should be useful also for this purpose, while its gating will be a bit tricky.

User avatar
Gavinmc42
Posts: 4296
Joined: Wed Aug 28, 2013 3:31 am

Re: WashbearyPi - Controlling a washing machine with a Pi

Fri Dec 27, 2019 2:06 am

A universal motor, did not know they still used those.
Could be an interesting machine to convert to off grid use.
Take DC voltage from solar panels/batteries or just replace with a DC motor.

Going to watch your progress with interest.
You should be able to control the speed via the stator coils?
I imagine the long one is the slow/start/wash coil and the short one is for spin?
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

askDrAlex
Posts: 11
Joined: Sat Dec 21, 2019 12:23 am

Re: WashbearyPi - Controlling a washing machine with a Pi

Fri Dec 27, 2019 7:52 pm

Gavinmc42 wrote:
Fri Dec 27, 2019 2:06 am
Going to watch your progress with interest.
I'm happy to hear that :D
Gavinmc42 wrote:
Fri Dec 27, 2019 2:06 am
You should be able to control the speed via the stator coils?
I imagine the long one is the slow/start/wash coil and the short one is for spin?
Indeed, the stator coils allow for a coarse speed control:
- Both (long + short) in series: Gives maximum torque, but lowest speed. Ideal to start moving the drum, especially when there is a lot of wet (=heavy) clothes inside.
- Long coil only: Medium torque, medium speed.
- Short coil only: Lowest torque, but highest speed. Ideal for spinning at top speed.

The fine speed control is done via the phase cutting method with a triac. That is something I still need to implement.

askDrAlex
Posts: 11
Joined: Sat Dec 21, 2019 12:23 am

Re: WashbearyPi - Controlling a washing machine with a Pi

Sun Dec 29, 2019 12:25 am

I made a figure to explain the phase cutting method:
PhaseCutting.png
PhaseCutting.png (20 KiB) Viewed 1469 times

The AC line voltage is sinusoidal. To reduce the electrical power absorbed by the load, a part of each sine half-wave is cut away. Since triacs are easy to turn on (just apply a gate current), but difficult to turn off (most of them can`t be switched off externally), the usual method is not to gate the triac after a zero-crossing of the voltage, but to wait for a certain period of time and ignite the triac during the half-wave cycle. The later the gating pulse is sent to the triac, the lower the electrical net power into the load will be. When the next zero-crossing occurs, the triac returns to its non-conductive state. This is illustrated in the second plot. It is the usual operating principle of most lamp dimmers.

At the moment, I cannot apply the phase cutting method, for two reasons:
- I have no phase measurement yet. To apply the method, the controller needs to know when a zero-crossing of the line voltage occurred.
- The MOC3081 optocoupler that I used in my circuit has a built-in zero-crossing detector and does only accept gating pulses that occur shortly after a zero-crossing. This has advantages in terms of EMI issues when the triac is used as a simple on/off switch, but prevents usage for phase-cutting purposes.

Accordingly, I will need to get another optocoupler, and add a phase measurement before I can do phase-cutting.
Still, there is a rather quick&dirty way of controlling the motor speed with the present setup. It is a kind of PWM method for AC voltage, see third plot in the above figure. What I can do, is to select a limited number of half-waves that arrive to the load, while all other half-waves are cut away. Also this method allows for power control. The drawback, however, it that it is rather "bumpy". For 10 milliseconds (= duration of a half-wave cycle at 50 Hz), the load gets full power, while not getting any electrical power in the following tens of milliseconds. I used this kind of power control for the washing machine motor. It works, but each half-wave gives the motor shaft and the drive belt a heavy kick, strongly loading the mechanics of the machine. I should upload a video soon, to show you how bumpy the motor acceleration actually is.

User avatar
Gavinmc42
Posts: 4296
Joined: Wed Aug 28, 2013 3:31 am

Re: WashbearyPi - Controlling a washing machine with a Pi

Sun Dec 29, 2019 12:41 am

Decades ago when i did this, I never did find a good zero crossing detector circuit.
It takes a few volts to trigger them so the timing is always a little behind.
I think I used bidirectional opto couplers?
But the timing was easy with a microcontroller back then.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

askDrAlex
Posts: 11
Joined: Sat Dec 21, 2019 12:23 am

Re: WashbearyPi - Controlling a washing machine with a Pi

Sat Jan 04, 2020 9:08 pm

The design of a zero-crossing detection circuit is just work in progress. My first version will use a transformer (230V AC -> 15 V AC), a voltage divider and a comparator IC. It is quite likely that the transformer will generate a slight phase shift. However, if the phase shift is constant for all operating conditions, it is not more than a constant offset that needs to be taken into account in the control algorithm.

I also thought about using a voltage divider and a comparator IC on the "hot" side and performing galvanic isolation after the comparator. This circuit would be intrinsically free of any spurious phase delays. The trouble, however, is that the comparator IC needs a low DC supply voltage, so one would either have to include another voltage divider + a bridge rectifier + a buffer capacitor, or use an extra switching power supply that delivers e.g. 5V DC which is floating relatively to the line voltage potentials. Presently, I think it is better to account for a phase shift, rather than making this extra effort.

Meanwhile, I uploaded another (preliminary) video showing the result of controlling the motor via the "half-wave selection method". It illustrates how rough the resulting motor acceleration is:

https://www.youtube.com/watch?v=5i33WHbAwCs

I am likely to replace that video by another one, once the phase cutting approach works.

Brandon92
Posts: 791
Joined: Wed Jul 25, 2018 9:29 pm
Location: Netherlands

Re: WashbearyPi - Controlling a washing machine with a Pi

Sun Jan 12, 2020 7:01 pm

askDrAlex wrote:
Sat Jan 04, 2020 9:08 pm
The design of a zero-crossing detection circuit is just work in progress. My first version will use a transformer (230V AC -> 15 V AC), a voltage divider and a comparator IC. It is quite likely that the transformer will generate a slight phase shift. However, if the phase shift is constant for all operating conditions, it is not more than a constant offset that needs to be taken into account in the control algorithm.
If you know the phase delay that you would get via the transformer. You could change the "voltage diverder" to overcome that. For example, normal you want that the comparator is active at 0V. But you could also set it -1V (for example). You could also use a windows detector for this.
askDrAlex wrote:
Sat Jan 04, 2020 9:08 pm
I also thought about using a voltage divider and a comparator IC on the "hot" side and performing galvanic isolation after the comparator. This circuit would be intrinsically free of any spurious phase delays. The trouble, however, is that the comparator IC needs a low DC supply voltage, so one would either have to include another voltage divider + a bridge rectifier + a buffer capacitor, or use an extra switching power supply that delivers e.g. 5V DC which is floating relatively to the line voltage potentials. Presently, I think it is better to account for a phase shift, rather than making this extra effort.
This option would also work. But you could also use a galvanic isolated power supply and a digital galvanic isolated IO. Analog devices has some nice IC for that: https://www.analog.com/en/parametricsearch/11037

But your the delay that you would get in the RPi would maybe a bigger problem.

askDrAlex
Posts: 11
Joined: Sat Dec 21, 2019 12:23 am

Re: WashbearyPi - Controlling a washing machine with a Pi

Mon Jan 13, 2020 9:28 pm

Setting the comparator threshold to a value other than 0V is an interesting proposal. However, this would result in a non-50% duty cycle of its output signal. You might compensate the phase delay on the rising edge of the sinusoid, but would make it even worse on the falling edge.
One would probably prefer to go for a comparator with hysteresis, which switches off e.g. at -1V, and switches back on at +1V. That would allow for phase shift elimination on both, rising and falling edges, by fine-tuning the hysteresis.

The ADUM series by Analog Devices - that is indeed a good point! Some of them come with a built-in, isolating DC-DC-converter. That would allow for DC voltage supply of the comparator on the "hot" side, and signal transfer back to the low-voltage side. If I had not started soldering on my transformer-based solution already, I could indeed have chosen this path.

I am about to use a 230V AC -> 15V AC transformer and a comparator. The comparator output will be fed into a GPIO pin of the Pi. On every rising and falling edge, the Pi will perform a gettimeofday and write the time stamp into a shift register. The sliding average of the register content will give me the frequency. Each half-wave cycle will then be divided into 256 equidistant time intervals. An 8-bit binary number (0-255) will then control the time at which the triac gets ignited.
If I have no phase shift, 0 should be full power (igniting early) and 255 should be minimum power (igniting late).
If there is a phase shift (+ jitter) in the zero-crossing detector, it may happen that 20 will be full power, 17 will be minimum power, and 18 and 19 will be something non-deterministic. So if the phase shift is static over time, it will not be more than an offset.
I will keep you updated on the outcome.

Brandon92
Posts: 791
Joined: Wed Jul 25, 2018 9:29 pm
Location: Netherlands

Re: WashbearyPi - Controlling a washing machine with a Pi

Tue Jan 14, 2020 8:50 pm

I would advice you to use a low pas filter in front of the comparator (or at his output) that would filter out the high frequency noise and other crap. However there is already some filtering, because of the transformer. When you use a window comparator you could set it to 1V and -1V. You get a a pulse that you could use to start your timer.

I know that the input of the Rpi is not "realtime". I mean there is some overhead when the input is changing to a other state. And I don't know if this is a problem for you.
I used the fuction callback to detect how low a input is active. And when the input was changing fast, the program wouldn't follow it.

Code: Select all

#Start the detection of either edge		
cb1 = gpio.callback(gpio.x, pigpio.EITHER_EDGE, TF_active)
I could share more of this code if you want.

askDrAlex
Posts: 11
Joined: Sat Dec 21, 2019 12:23 am

Re: WashbearyPi - Controlling a washing machine with a Pi

Sat Feb 01, 2020 11:48 pm

After more than 2 weeks, here finally comes an update:

I built and tested the transformer-based phase detection circuit in the mean time. Here is the circuit diagram (including the triac part):
triac2.png
triac2.png (30.58 KiB) Viewed 781 times
It turned out that the LM2901 comparator does quite strange things when the input potentials at IN+ and IN- are floating relatively to the power supply pins of the IC. That is why the addition of the voltage divider R7 / R8 turned out to be necessary, which clamps the potential at the IN+ pin to 2.5 V. Without that, I got anything but a 50% duty cycle in the comparator output.
The R5 / R6 voltage divider ensures that the IN- potential does not go below GND potential, and not above 5V (which the comparator would not like very much according to its data sheet).
Since the LM2901 comparator has open-collector outputs, the pull-up resistor R9 is required to get TTL output levels. I know that the Pi's GPIOs are designed for 3.3V, but through a 100kOhm pull-up resistor to 5V, I won't kill any GPIO pin, since the current is limited to 50 microamps.
R10 is a "reverse data direction protection resistor". Should the corresponding GPIO pin accidentally be configured as an output by software, and be set to HIGH while the LM2901 pulls it down to ground, R10 will limit the short-circuit current to a value below the damage threshold of the GPIOs.

Having built this circuit, I connected it to 5V and 230V AC, and checked the comparator output with an oscilloscope. Result (5ms/div, 2V/div):
comparator_output1.jpg
comparator_output1.jpg (76.61 KiB) Viewed 781 times
It is a nice and clean TTL signal, and there is no indication for any multiplie HIGH/LOW switching around the edges. When I connect the comparator output to the Pi and operate it all on 5V from my lab power supply, it still looks good. However, it turned out that when I power this circuit and the Pi with the low-cost Chinese plug power supply that I routinely use for the Pi, quite strange things happen (5ms/div, 2V/div):
comparator_output2.jpg
comparator_output2.jpg (78.76 KiB) Viewed 781 times
The edges look washed-out (and have multiple HIGH/LOW switching issues), but what is more severe: High-frequency noise shows up in the middle of the pulses! Zooming further in revealed that this is some 50-70 kHz noise - a very typical frequency for a switching power supply. So indeed, the circuit has issues with noise pick-up from peripheral devices. I will have to see how to get rid of that. A few extra capacitors might help.

The good news at the moment is that there does not seem to be a major phase delay. The rising and falling edges of the comparator output signal coincide with zero-crossings of the AC voltage quite well.

askDrAlex
Posts: 11
Joined: Sat Dec 21, 2019 12:23 am

Re: WashbearyPi - Controlling a washing machine with a Pi

Sun Feb 02, 2020 11:50 pm

And this time, the next update comes within an day...

It turns out that despite noise issues, the circuit works very well as a dimmer for an AC lamp, as you can see in the following video:

https://www.youtube.com/watch?v=AAWoYxKtrBM

In case you want to use my code (C++, using WiringPi) for this or a similar purpose, here it is:

Code: Select all

#include <iostream>
#include <unistd.h>
#include <sys/time.h>
#include <wiringPi.h>
using namespace std;

int main () {

if (wiringPiSetup() == -1)
    return 1;

pinMode(15, OUTPUT); //controls the triac
pinMode(16, INPUT);  //reads from the comparator
//nota bene: WiringPi pin 15 = GPIO14, and WiringPi pin 16 = GPIO15

int result1, result2, result3, result, oldresult;
int delay=8000;  //zero-crossing to triac-ingition delay
int dir=0;

for (int i=0; i<40000; i++) {
  result1=digitalRead(16);
  usleep(1);
  result2=digitalRead(16);
  usleep(1);
  result3=digitalRead(16);
  if (result1==result2 && result2==result3) {  //only trust measurement if 3x consistent
    result=result1;
    if (result!=oldresult) { //in case we find a line-voltage zero-crossing...
      usleep(delay);  // ...wait for a period of time given by 'delay'...
      digitalWrite(15, 1); /// ...and ignite the triac
      usleep(100);
      digitalWrite(15, 0);
    }
  }
  oldresult=result;
  usleep(100);
  if (dir==0) {delay-=3;} else {delay+=3;} //ramp up and down 'delay' linearly
  if (delay < 500) {dir=1;}  // min. delay 0.5 ms (almost maximum power)
  if (delay > 9000) {dir=0;} // max. delay 9 ms (power close to zero)
}

return(0);
}
As yo can see, there is some noise suppression implemented in software. The comparator output is read three times in sequence, and only if the result is three times the same, the result is trusted.
Finally, I should mention that there is a tiny inaccuracy in the previously posted circuit diagram: The optocoupler triac is a MOC3051 this time, which does not have a zero-crossing switch (otherwise, phase cutting would not work). The device symbol, however, is not in line with that. It still shows a "1" in a box, which represents the zero-crossing switch. I could not find the correct symbol in my CAD program.

Return to “Automation, sensing and robotics”