Problem with analogRead and analogWrite


I have a problem that´s driving me crazy.

I have an temp sensor ( T0-92 ) reading the temp on A3. I have an Led driver with a PWM input connected to D9. The Led driver i grounded to the Arduino GND

When i connect the Led driver to the battery the PWM dimming works fine but the analogRead value from the Temp sensor start to flicker.

I have tried connecting a 10k resistor PWM to ground and Temp value still flickers. I have tried diffrent pins, 3,3V to temp sensor and still starts to flicker when connecting Led driver to battery.

I have measured the 5V pin on arduino before and after connecting the led driver and it´s the same 4.9V.

  Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

 This example code is in the public domain.

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:

// the loop routine runs over and over again forever:
void loop() {
  int analogoutpin = 9;
  // read the input on analog pin 0:
  int sensorValue = analogRead(3);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float temp = sensorValue * (4 / 1023.0);
  // print out the value you read:
  analogWrite (analogoutpin, temp);

your math seems odd to me.. Readings 0..1023 -> 0..255 ?

then loop can be:


This code was just some example code but dont mind the code itself.

I have tried with a Nano and still get the same flickering on the temp value.

I also have an light sensor but the value its reading works fine.

And i have tried a different Temp sensor.

It´s looks like the PWM signal for the led driver causing the problem. When i disconnect the pwm Pin the temp value is fine.

Best way to fix this i guess is to optoisolate the pwm in som way??

add a small capacitor to tempsensors powerline (1u..10u) Keep wires short