Can the Leonardo read PWM with analogRead?

Hello
I already have the Arduino Uno, and I just ordered the Leonardo so I can emulate keyboards. I'm working on a project that will have a lot of inputs, so I want to know if I can communicate between my Uno and Leonardo using analogRead and analogWrite.
So my question is: if I connect a PWM output from the Uno to an analog input on the Leonardo, and use analogWrite(someValue), will the Leo be able to analogRead about the same value?

charredgrass:
Hello
I already have the Arduino Uno, and I just ordered the Leonardo so I can emulate keyboards. I'm working on a project that will have a lot of inputs, so I want to know if I can communicate between my Uno and Leonardo using analogRead and analogWrite.
So my question is: if I connect a PWM output from the Uno to an analog input on the Leonardo, and use analogWrite(someValue), will the Leo be able to analogRead about the same value?

No, not with direct wiring. The pwm analogWrite function is a digital signal only (always either high or low) with it's duty cycle the only variable. An analogRead function reads a continuous analog voltage level in the range of 0vdc to +5vdc on a Uno. To do what you wish would involve adding an external low pass filter to change the pwm signal to a true analog signal.

Lefty

Have a read of this to see what PWM gives you:-
http://www.thebox.myzen.co.uk/Tutorial/PWM.html

PWM changes from 0 to +5 at ~490Hz, with the narrowest on or off time being 1/256 of 490Hz and the widest pulse being 255/256 of 490Hz.
(1/490)/256 = 7.97uS

More accurate to use the pulseIn() command to read the low time, and the high time, add the two together to get the switching period.
Not sure how small a time pulseIn() can measure, check the reference page.
delayMicroseconds() can be as low as 4uS, so I'm guessing 8uS should be measurable.
Give it a try - PWM runs continuously as a free running hardware feature, so you should be able to use pulseIn() to measure a devices own PWM outpout.

There are much better ways to accomplish serial communications between Arduinos.

Agreed, using softwareserial would be more efficient to send a number 0-255 from one device to another.
That likely ties up two pins on each device tho. analogWrite on one and analogRead (or pulseIn) on the other only commits one.

You still need some kind of "ready" line. Otherwise, when does the receiver know when the signal has stabilized?

What about the fact that the PWM aka analogWrite varies with Vcc? As does analogRead? Are both Arduinos running on the same Vcc? What about noise on Vcc and ground? That is about 19.5mV per step from 0 to 255, but it only takes a fraction of that in noise or other variation to make it read a different number.

Really depends on what info you're trying to get across, doesn't?
If you're merely trying to pass along a number from 0-255, then a steady PWM signal that the receiver can do pulseIn() on a couple of times will be pretty simple. The host can do a short burst at some PWM value, then 0, then another burst, then 0, etc.
Same uC that is receiving can read the pulse widths pretty quick and make a determination of the number represented.
Trying to pass an analog signal along, I'd agree, Vcc differences on both ends could make a difference in readings if there were no calibration type readings taken to get them in sync.

Thanks for all the help!
I think I'm going to use softwareSerial, but do you know how long it takes to send the signal? It should be fast enough to send the inputs of several push buttons, right?

EDIT: Never mind, stupid question, it will be fast enough for my project.

It will be plenty fast enough for that. After all, how fast can you move your fingers?

Code can be something like this.
Assume you have buttons on PORTC, pins 0-1-2-3, enabled as inputs with internal pullups enabled and buttons that connect the pins to Gnd when pressed:

void loop(){
switchState = PINC & B00001111; // read the 4 pins, mask for the lower 4 bits
  if (switchState < B00001111) {  // is any button pressed? 
  softwareSerial.print (swithchState); // whatever the syntax is for softwareSerial
  }
}

On the receive side, PORTC bits 0-1-2-3 are set up as outputs:

switchState = B00001111;

void loop(){
  if (softwareSerial.available() >0){  // byte received?
  switchState = softwareSerial.read();
  PORTC =  switchState; // bits 0-1-2-3 follow what was sent over
// if LEDs are connected with Anode to +5, then cathode to current limit resistor to PORTC bits 0-1-2-3,
// the LEDs will light up/off as the buttons are pressed/released and sent over
  }
}

May need some syntax tweaking; this will seem like realtime to you it will be so fast.
Maybe PORTC is not the greatest with the actual hardware, I don't know ports the Leonardo has, but you get the idea.