1
votes

I'm doing a communication system with 2 Arduinos Leonardo. To transmit I had to convert a String in bits and send them trough a PWM pin. The question is, how can I do the receiver?

My idea is to receive data in a digital pin, so I can convert LOW/HIGH to '0' and '1', put 8 bits in a string, convert to char and then I have my final String.

Any help/suggestion would be appreciated.

EDIT:

  • I'm using PWM because I want to implement FSK modulation, sending bit by bit;
  • I have some hardware to get communication system working;
  • My physical channel for communication is Serial (via Arduino pins);
  • In the receiver hardware will provide me TTL signals, so I will have 0V/5V. That's why I think digitalRead() should work;
  • Data is sent via PWM and received in two level signals;
  • I'll have to do some protocol to send/receive data, still don't know how.. ;
3
Whatever you do, make sure to connect the grounds! If you don't, their definitions of 5V will be different causing unexpected behavior. (To put it simply). Also, why are you using the PMW pins? You can send data via non-PMW pins. PMW is just going ON-OFF very fast (I don't even know if an Arduino can read that: someone please back this up or argue this-I don't know.) This can simulate lower voltages to "Dim" a LED without wasting an analog pin.Anonymous Penguin
@AnnonomusPerson Check my edit info. What do you mean with "connect the grounds" ? PWM is working fine, at least I get data in serial monitor without errors.mafap
I mean literally run a wire from the ground on one Arduino to the other. You don't need to do this if it is wireless. Voltage is just a comparison to ground; if there is different grounds but the same amount of energy, they are going to be different voltages. It's hard to explain and I don't really understand it myself but someone had a problem hooking boards together on SO and connecting the grounds was the solution. Just a general rule of thumb, if different electrical components are connected by an electrical wire, connect the grounds. Running out of room & time and will explain more later.Anonymous Penguin
Why do you want to have the two Arduinos communicate via FSK?angelatlarge
Actually I want more than 2 Arduinos to communicate. It will be something like a ring link in wireless, what means that each Arduino will be a receiver/transmitter.mafap

3 Answers

1
votes

You don't say what your physical channel for communication is. This is important. On Arduino your main options are:

You can also implement your own protocol by writing and reading data to/from digital pins, though more likely than not this will look like SPI. None of these methods necessitate converting anything into a String, unless you want to send a string. I am a bit at a loss how you are sending data via PWM exactly: it seems like it would be an unwarrated hack.

1
votes

Using the PWM is the hard way to communicate binary data between device, in lieu of the other more readily available peripherals, but doable. Basically PCM or Pulse Code Modulation. The simplest way for this would be the length of time the pin is asserted, represents the data. Which for example is what happens for servo's. Where the PWM is repeating the same length of time that represents a known position and the Servo's receiver converts the length of the pulse back to position or data.

The reception of PWM is not typical for Arduino users, But the AVR micro is more than capable of this. There are two possible ways to capture such pulses duration. First and common to Arduion is the PulseIn() function. Where this function is not real time. It simply time stamps the cycle between pulses and limited to duration of only a few microseconds. I believe there are others variations that may go longer. This can work very good. but may have issues with other interrupts such as Serial functions and timers.

Another method is known as Input Capture Interrupt. The following GISTHUB INputCapture.ino is an example of such code that I salvaged from another source. Being not typical, the code is not written as a Arduino Library. But does very accurately captures the length of a pulse. In short the Input Capture pin hardware locks on the system clock on trigger and then at some latent time it can be read.

0
votes

Well, this is the code I wrote:

void setup() {
  Serial.begin(9600);
  while (!Serial) {;}
}
void loop() {
  char bitPosition = 0;
  String inMessage = 0;
  while (Serial.available() > 0) {
    char outChar;
    boolean digValue = digitalRead(inPin);
    for(bitPosition = 7; bitPosition >= 0;  ){
      if(digValue == LOW){
        bitWrite(outChar, bitPosition, 0);
      }
      if(digValue == HIGH){
        bitWrite(outChar, bitPosition, 1);
      }
    bitPosition--;
    }
  inMessage.concat(outChar);
  if(inMessage != 0){
    Serial.println("Received:  " + inMessage);
  }
 }
}

this is not giving what i was expecting. The first digValue is always 0. Sugestions?