I am trying to use an optocoupler with my CNC to control the spindle speed on the Chinese speed controller with PWM output from my RAMPS after seeing an article about it here. The controller uses a potentiometer to control the speed via voltage across the sliding contact o the pot. I was hoping to use an optocoupler to mimic this voltage through PWM.
Hence I decided to experiment with this optocoupler a bit to see if I could get it to transmit PWM signals which could be read as analog values.
I referenced this article on optocouplers and hence followed the recommended size of resistors (200 for the anode and 1k pullup for the collector.
I set up my circuit (diagram attached) with one Nano generating and another measuring PWM signals.
Here is the code I used:
Transmitter:
//Transmitter code
byte testpin = 9;
String message = "";
int Value = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Ready");
pinMode(testpin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
for(int i=0; i<= 255; i++){
analogWrite(testpin, i);
delay(1000);
Serial.println(i);
}
for(int j=255; j>=0; j--){
analogWrite(testpin, j);
delay(1000);
Serial.println(j);
}
}
And for the receiver:
//receiver code
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(analogRead(A0));
delay(100);
}
I tried using input pullup on the A0 pin for the receiver as well as a hardware 1k pullup. However both times I got a quirky result on the receiver via serial plotter (image attached).
Is there something I am missing in my code and setup?
I am rather new to electronics so any advice would be greatly appreciated!
PWM isn't an anlog signal; it's as digital as can be.
That's exactly what you see in your plot.
Do you fully understand what PWM means and what such signals would look like ?
To approach an analog signal, you need to smooth out the results at the output of your opto.
You need a capacitor for that.
Also, your setup will invert the output.
Let's say you're sending a PWM signal value of 50.
Measuring this with a digital multimeter at the Arduino output, would result in a value close to 0.98 volts
The inverted output will then result in a measured value that is close to 205, or 4.02 volts.
It's very easy to solve this, without any hardware but you need to be aware of it.
PWM isn't an analog signal; it's as digital as can be.
That's exactly what you see in your plot.
Do you fully understand what PWM means and what such signals would look like?
I see. I was expecting the ADC of the receiving nano to be able to convert the "faked" voltage of the PWM signal into an analog value
MAS3:
To approach an analog signal, you need to smooth out the results at the output of your opto.
You need a capacitor for that.
but this clarifies it. So am I right to say that unlike an appliance like a motor or an LED, the ADC of the nano wasn't "tricked" into measuring the average voltage of the PWM duty cycle?
None of the items you mentioned are tricked to see some average.
A LED will blink along with the PWM's frequency.
It's the human watching that LED's light who is tricked to see an average of it's brightness.
A digital volt meter can be "tricked" into presenting an "averaged" voltage for a PWM output if the frequency is high enough. With my cheap one, anything over about 0.2Hz and it will start integrating the voltage with respect to time. So a 5Volt output with a duty cycle of 25% would appear as 1.25 volts. An oscilloscope, on the other hand, would not be "tricked" at normal PWM frequencies and should accurately display the PWM waveform.