FastLED with analogRead() does't work

Hello Everybody,

I’m using UNO R3 & WS2812B ledstrip.
I want to control LED’s hue by an external potentiometer.
So I wrote a following code. But it doesn’t work.
If “USE_POT” was commentouted, it works fine and I could check hue value from serial monitor.

What am I doing wrong?

#include "FastLED.h"

// if defined, hue is got from POT by using analogRead(). 
#define USE_POT

#define DATA_PIN 11
#define NUM_LEDS 60

CRGB leds[NUM_LEDS];

int potPin = 0;   // Pin for potentiometer
int potVal;
uint8_t hue;

//---------------------------------------------------------------
void setup() {
  delay(200);
  pinMode(potPin, INPUT);  // Set pin as an input.

  // for debug
  Serial.begin(9600);  // Allows serial monitor output (check baud rate)

  hue = 128;
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.clear();
  FastLED.show();
  delay(500);
}


//---------------------------------------------------------------
void loop() {
  
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CHSV(hue, 255, 64);  // hue comes from pot A, and brightness value is scaled based on pot B.
  }
  FastLED.show();
  Serial.println("FastLED has called");
  delay(500);

#ifdef POT_USE
  //-- use analogread, NOT OWRK
  potVal = analogRead(potPin);
#else
  //-- WORK FINE
  potVal = (int)random(0, 1023);
#endif

  hue = map(potVal, 0, 1023, 0, 255);
  Serial.println("chekKnobs has called");
  Serial.print("pot: "); Serial.print(potVal); Serial.print("  hue: "); Serial.println(hue);

  delay(500); 
}

Maybe your potentiometer is connected to the wrong pins. Hard to tell.

As already stated, be sure you have the correct input pin.

The pot should be connected as below:

  • One end of the pot => 5V
  • the wiper (center) => analog input pin
  • Other end of the pot => gnd

[u]Be sure you have this correct!! If you accidentally reverse the wiper and ground you will damage the pot.[/u]

Do you have a multimeter? If not I would suggest you look into getting one. Even a cheap one would help in this instance. If you do, verify the analog input pin has a voltage you can set with the pot.

define USE_POT

ifdef POT_USE

Leo..

Thank you for replies.

I had fixed the source.

ifdef POT_USE --> #ifdef USE_POT

and commented out. //#USE_POT --> #USE_POT

Then compiled, uploaded and ran the sketch.

I could confirm analogRead() and hue values from the serial monitor, but ledstrip color did't change.

I wander that FastLED.show() doesn't work correctly after analogread() was called.

I wander that FastLED.show() doesn't work correctly after analogread() was called.

That simply is not the case. Please post the code again with your corrections and then we can try it for ourselves.

Please post the code again with your corrections and then we can try it for ourselves.

The code is here.
Thank you.

#include "FastLED.h"

// if defined, hue is got from POT by using analogRead(). 
#define USE_POT

#define DATA_PIN 11
#define NUM_LEDS 60

CRGB leds[NUM_LEDS];

int potPin = 0;   // Pin for potentiometer
int potVal;
uint8_t hue;

//---------------------------------------------------------------
void setup() {
 delay(200);
 pinMode(potPin, INPUT);  // Set pin as an input.

 // for debug
 Serial.begin(9600);  // Allows serial monitor output (check baud rate)

 hue = 128;
 FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
 FastLED.clear();
 FastLED.show();
 delay(500);
}


//---------------------------------------------------------------
void loop() {
 
 for (int i = 0; i < NUM_LEDS; i++) {
   leds[i] = CHSV(hue, 255, 64);  // hue comes from pot
 }
 FastLED.show();
 Serial.println("FastLED has called");
 delay(500);

#ifdef USE_POT
 //-- use analogread, NOT WORK
 potVal = analogRead(potPin);
#else
 //-- WORK FINE
 potVal = (int)random(0, 1023);
#endif

 hue = map(potVal, 0, 1023, 0, 255);
 Serial.println("chekKnobs has called");
 Serial.print("pot: "); Serial.print(potVal); Serial.print("  hue: "); Serial.println(hue);

 delay(500); 
}

I used this:-

#ifdef USE_POT
 //-- use analogread, NOT WORK
 potVal = analogRead(potPin);
 potVal = 42;
#else
 //-- WORK FINE
 potVal = (int)random(0, 1023);
#endif

And it worked fine at a steady red.

I haven't got a pot wired up at the moment, but as you can see the analogRead function is being called. Therefore I suggest your pot has not been wired up correctly.

I tried your code and it didn't work. LED color didn't change from initial light green.

I doubt my hardware have flaws. So I ordered another arduino board. When I receive it, then I'll try again.

Are you saying that bit of code did not produce a steady red? If not you have something not wired up correctly.

Try another analogue input pin.

Yes, it did not produce a steady red. And when I commented out analogRead(), it produced a steady red! It's strange that calling analogRead() affects FastLED's functions.

It's strange that calling analogRead() affects FastLED's functions.

Well strange is not the word for it. I think you are missing something but I don't know what. Can you post a picture of your wiring please.

Grumpy_Mike: Well strange is not the word for it. I think you are missing something but I don't know what. Can you post a picture of your wiring please.

I took two movies, without and with analogRead(). https://www.youtube.com/watch?v=TyqvDjRXeBY https://www.youtube.com/watch?v=2z8XD4uDoMs

I can only say it's weird.

you should post a high resolution image of all your connections.

Those links do not work when you click on them, they are just blank but the label is valid. What ever you do when you post them you are doing it incorrectly.

Yes we need to see the wiring. In particular the common ground between the LED's power supply and the Arduino.

Just did a video of what I get when I use your code on my system. https://youtu.be/e7Td2K1peDo

See comments in the video. If you set the hue first, then set and show the LEDs, then delay, you will not get a delay between turning the knob and seeing the colours change.

I fixed the links. Now they work.

You need to power the strip from a separate power adapter. USB cab only provide 500mA of current.

Connect the ground of the power supply to the ground of the arduino.

60 * 20ma red * 20ma blue * 20ma green = 3600ma total.

I am surprised you are not blowing the fuse on your USB port...

I must apologize. I found my wiring mistake, I connected strip's GND to arduino's AREF.

Now my prototype works fine. Thank you very much.