Read analog signal from one arduino to another

I am working on an arduino synthesizer and I plan on being able to use multiple modules together. Part of this would require me to be able to read the analog and digital voltage from one to another.

What would be the way to go about connecting the two? Would I just need to connect both grounds together to complete the circuit?

Sorry if this is a newb question. I don't know much about electronics.

Here's a picture and audio example of what I have so far if anyone is curious:
Imgur

maybe u could just connect them via their I2C pins?

u could just connect 1 digital pin of arduino#1(transmitter) to arduino#2(receiver) and say "beeeepbeep" for "1" and "beepbeeeep" for "0" and "beeeeeeeepbeeeeeeeep" for "start of transmission" and "beepbeep" for "end of transmission"...

where the first "beep"/"beeeep" is HIGH and the second is LOW and the number of "e"s denote the length of the signal...

const int8_t tB = 8;
void tx(uint8_t pin, uint8_t tH, uint8_t tL) {
  digitalWrite(pin,HIGH);
  delayMicroseconds(tH*tB);
  digitalWrite(pin,LOW);
  delayMicroseconds(tL*tB);
}
void txByte(uint8_t b) {
  tx(10,10,10);
  for (uint8_t i=8; i>0; i--,b>>=1)
    if (b&1)
      tx(10,2,1);
    else
      tx(10,1,2);
  tx(10,1,1);
}
void rx(uint8_t pin, int8_t& tH, int8_t& tL) {
  tH = pulseIn(pin,HIGH,100);
  tL = tH ? pulseIn(pin,LOW,100) : 0;
}
int16_t rxByte() {
  int8_t tH, tL;
  for (uint8_t try=100;; try--) {
    rx(10,tH,tL);
    if (tH > 5*tB && tL > 5*tB)
      break;
    if (!try)
      return -1;
  }
  uint8_t v = 0;
  for (uint8_t i=8; i>0; i--) {
    rx(10,tH,tL);
    if (tH > 3*tB || tL > 3*tB || tL == 0)
      return -1;
    const int8_t dt = tH - tL;
    if (dt > tB/2)
      v = (v>>1) | (uint8_t)128;
    else (dt >= -tB/2)
      return -1;
  }
  rx(10,tH,tL);
  if (tL == 0 || abs(tH-tL) > tB/2)
    return -1;
  return v;
}

i didnt test that... :slight_smile:
maybe some parity would be a nice idea, too...

It's not clear what you mean by "read from".

Do you want to generate an analog signal on one Arduino, and have another be able to convert it with an A-to-D converter?

Yes, you need to connect the grounds together: except for a few rare cases, you always need to connect the grounds of all parts of a system.

Ran

Sadly since I am generating two square waves at once already on the synthesizer arduino I cannot afford to have many loops within it for data transfer.

Imagine that I have two units. To keep it simple, one unit is generating a square wave at a rate that's based on how much voltage is coming from a call to analogRead() on a ANALOG IN pin. The other unit is providing a sequence of voltages (let's just say I'm using PWM to make it easy) that will be sent to this pin.

From the sound of it, if I want it to work, I just need to connect a PWM voltage of one arduino to another's ANALOG IN pins and also connect each other to the same ground.

I'm pretty sure you answered it already but I wanted to clear up any confusion. So thanks!

I just need to connect a PWM voltage of one arduino to another's ANALOG IN pins and also connect each other to the same ground.

Alas, no, it's not that simple.

You'll need to filter that PWM output to turn it into an analog signal. Otherwise, the second Arduino will just see a "0" or a "1023", depending on where in the PWM cycle it samples the input.

If you're not using that analog output for some other purpose, it would be much better to communicate the digital value using a mechanism like the serial port or I2C (the Wire library). Otherwise, you're definitely going to lose some accuracy getting the signal from one CPU to the other, and it may be enough to cause problems.

The serial port has a buffered receive that will accumulate data "in the background", so it can be used with minimal disruption of your square wave generation.

Ran

Alas, no, it's not that simple. ...

Ah well I already have done something like this. The analog outputs of what I have so far are constant, they are not using PWM. I was hoping the PWM example would have been easier but I wasn't thinking about how they actually work.

I must thank you, though, for answering another question I've had in my head for a while about serial and how it accumulates data. Of course if by "in the background" you mean that it doesn't interrupt the loop. Maybe I am just biased because whenever I'm debugging and use Serial.println() I get some tick sounds in my output (as in it's taking a while to execute the printing code).

Only serial input is buffered and interrupt-driven: the output code is blocking, only transmits one character at a time as the hardware transmit register empties, and will tie up the CPU until everything is sent.

There's some discussion going on about adding a transmit buffer, now that many people are moving to the ATMega328 with more RAM. But it hasn't been done (or even started, as far as I know) yet.

Ran