1
votes

I am pretty new to Arduino and am trying to figure out how to connect 2 pins, then output information (or PWM) from one and input it on the other pin.

So far I tried this but it does only show the same numbers (~500 to 440), even if I disconnect the pins:

int pin_out = 9;
int pin_in = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {

  analogWrite(pin_out, 255);
  delay(500);
  Serial.println(analogRead(pin_in));
}

Edit:

I managed to measure some signal with this code:

int pin_out = 9;
int pin_in = 0;

void setup() {
  Serial.begin(9600);
  pinMode(pin_out, OUTPUT);
  pinMode(pin_in, INPUT);
}

void loop() {
  analogWrite(pin_out, HIGH);
  Serial.println(pulseIn(pin_in, HIGH));
  delay(500);
  analogWrite(pin_out, LOW);
  Serial.println(pulseIn(pin_in, HIGH));
  delay(500);
}

The signal ranges from 0 to 9. What am I measuring?

2
You can't read a PWM signal with analogRead.gre_gor

2 Answers

2
votes

If you are using PWM, use the pulse() function to measure it. Analog Read can't help you there.

This is an interesting question. Perhaps try using pins far away from each other, as atmospheric noise can disrupt the signal.

1
votes

Communication between two pins may be tricky, PWM signal is used mostly to communicate with motors or devices which needs input values from <0, 2000> range.

Make sure that your Arduino pin may generate PWM signal with analogWrite. In most arduino boards pins which may be used as PWM output are marked with white circle around it. If you cant see any of them check it in documentation.

In my opinion if you really want to communicate with 2 pins with PWM alike signal you should use Servo.h library. With write() method you may generate PPM signal easily. You may read it with pulseIn function but I would recommend using interrupt, as it is way more faster.

Actually there is created already 2 pins communication protocol named UART. You may try softwareSerial or something like that if u want to send more than <0, 2000> value. I2C is using 2 pins either. I would recommend one of them.