0
votes

I'm trying to Read values from LM35 sensor on Arduino UNO and send it to another Arduino board through a PWM pin and an analog Pin

enter image description here

When I run this project, The Serial Emulator of Arduino A is showing right values but Second one is always 0.00. Here is my first Arduino Code:

int pin = 2;
int TempPin = A0;
int pinAnalog = 3;

void setup() {
   pinMode(3, OUTPUT);
   Serial.begin(9600);
}
void loop() {
   float tmp = analogRead(TempPin);
   float Result = (tmp/1024.0) * 500;
   Serial.println(Result);
   analogWrite(pinAnalog, Result);
   delay(3000);
}

And Here is My Second Arduino Code:

void setup() {
    Serial.begin(9600);
}
void loop() {
    float res = analogRead(A0);
    Serial.println(res);
    delay(3000);
}

What's wrong with my project or code?

1
I guess C1 = 10nF is far too small to make a useful RC for the PWM frequency. Do you have a multimeter or oscilloscope? BTW: This does not seem to be a programming question.datafiddler
float is the wrong datatype: analogWrite takes a byte ( 0 .. 255 ) as value. analogRead returns an int.datafiddler

1 Answers

1
votes

I understand this is an exercise only, as PWM itself is not suitable to feed analogRead. (better measure pulse durations, if you really want to use it for data transmission.)

For a 400 Hz PWM you need a RC Value of e.g. 20 ms to filter the PWM pulses reasonably.

(e.g 1µF * 20k)

As you work in a 3sec Cycle, much bigger values are fine as well.

BTW: Sender could be simplified to:

const byte inPin = A0;
const byte outPin = 3;

void setup() {
   Serial.begin(9600);
}
void loop() {
  byte tmp = analogRead(inPin)/4;  // 0 .. 255
  analogWrite(outPin, tmp); 
  Serial.println((int)tmp);
  delay(3000);
}