Using Potentiometer to control RGB LED and Continuous Servo?


I'm trying to get a single potentiometer to control the spin rate of an FS90R continuous servo, and have an RGB LED change color based on the spin direction. I am having issues getting both functions to work...if I comment out the LEDcolor(), the motor works fine, but if the LEDColor() is specified the motor does not spin. Could anyone point me in the right direction as to what I'm doing wrong?

Thank you! Code is below.

#include <Servo.h>
Servo myservo;  

int potpin = A0;  
int val;    // variable to read the value from the analog pin

int R = A3; //pin for red LED
int G = A4; //pin for green LED
int B = A5; //pin for blue LED

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(R, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(B, OUTPUT);

void loop() {

       val = analogRead(potpin);           
       int servospin = map(val, 0, 1023, 0, 180);  

       Serial.print("servospin is ");
       Serial.print("val is ");
       if (servospin < 90) {
         Serial.println("red light");
       else if (servospin > 90) {
         Serial.println("green light");
void LEDcolor(int R_RED, int G_GREEN, int B_BLUE)
  analogWrite(R, R_RED);
  analogWrite(G, G_GREEN);
  analogWrite(B, B_BLUE);

I'm confused... analogWrite() is not for the Ax pins which are analog inputs; it's for those digital pins which have the ~ mark on some Arduinos. (And using the servo library, you lose that ability on some pins, I forget which right now.)

Seeing as you only use 255, you might as well do a digitalWrite() to high anyway...

What Arduino board are you using?

On the Uno and Nano the pins A3, A4, A5 are not PWM (analogWrite) pins. You can use digitalWrite(pin, state); with those pins, though.

Pins 9 and 10 on the boards with mega328 processors.

As others have stated... somewhat confusingly you have to use digital pins for analogWrite... and even then only the ones that support PWM. PWM effectively lowers the output level by changing the duty cycle (5v/0v) of the output.

Don't know why the motor stops spinning however... how is everything connected? DO you have a schematic?

Thanks!! I am using an Adafruit Feather M4 Express, here is a schematic of how I have it set up. Despite being in the analog pins the light is working as it's supposed to? It just shuts down the motor if I leave the LEDcolor() code in..

oops just realized i left off the servo...the one I am using is the continuous FS90R though, not this one.

Now might be the time to migrate from Fritzy images to a circuit diagram.

What is your power supply?
Your image shows the servo connected to the M4 3V3 pin, do you know how much current that pin can supply?

Feather M4:

  • Measures 2.0" x 0.9" x 0.28" (50.8mm x 22.8mm x 7mm) without headers soldered in
  • Light as a (large?) feather - 5 grams
  • ATSAMD51 32-bit Cortex M4 core running at 120 MHz, 32-bit, 3.3V logic and power
  • Floating point support with Cortex M4 DSP instructions
  • 512 KB flash, 192 KB RAM
  • 2 MB SPI FLASH chip for storing files and CircuitPython code storage.
  • 32.768 KHz crystal for clock generation & RTC
  • 3.3V regulator with 500mA peak current output
  • USB native support, comes with USB bootloader and serial port debugging
  • Built in crypto engines with AES (256 bit), true RNG, Pubkey controller
  • Tons of GPIO! 21 x GPIO pins with following capabilities:
    • Dual 1 MSPS 12 bit true analog DAC (A0 and A1) - can be used to play 12-bit stereo audio clips
    • Dual 1 MSPS 12 bit ADC (6 analog pins some on ADC1 and some on ADC2)
    • 6 x hardware SERCOM - Native hardware SPI, I2C and Serial all available
    • 16 x PWM outputs - for servos, LEDs, etc
    • I2S input and output
    • 8-bit Parallel capture controller (for camera/video in)
  • Built in 100mA lipoly charger with charging status indicator LED
  • Pin #13 red LED for general purpose blinking
  • Power/enable pin
  • 4 mounting holes
  • Reset button


General specifications

Digital?: N
No-load current @ 6V: 120 mA
Stall current @ 6V: 800 mA1
Speed @ 6V: 130 rpm
Stall torque @ 6V: 21 oz·in1
Speed @ 4.8V: 100 rpm
Stall torque @ 4.8V: 18 oz·in1
Hardware included?: Y
Lead length: 10 in

1 Stalling should generally be avoided as it can damage the motor and gears.

Tom... :smiley: :+1: :coffee: :australia: