Servo Photon Issue please help!

I’m currently developing a web application to control an electric longboard. I was able to obtain all my functionality in my initial set up with Arduino, but when I migrated from Arduino to photon my code no longer worked.

Description of my setup:
I have the 2.4Ghz Rc remote receiver plugged into my photon shield shield I need the 5v output. I confirmed that my photon was receiving the correctly by turning on/off the on board light. I knew the range of values (1400-2100) that my remote was inputting into the photon, by varying throttle. I confirmed this using the Serial monitor on Arduino. The receiver would normally be plugged directly into an ESC. But in order to limit the speed, I’m having pass through the photon so that I can limit the maximum value which occurs when the throttle is fully depressed.

This is my Arduino Code that functions like a charm!

 #include <Servo.h>
  
  Servo motor;
  int rcPin = 10;
  int val;
  int limiter = 1600;
  
  void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    motor.attach(3);
    delay(1);
  }
  
  void loop() {
    // put your main code here, to run repeatedly:
    val = pulseIn(rcPin, HIGH);
      
      if(val > limiter) {
      motor.write(limiter);
      } 
      else {
        motor.write(val);
      }
    
  }

I transferred the code to my photon (removing the library and assigning the pins correctly) and I get no response from my esc. I’ve been reading online and it seems that this may be caused by the differences between the PWM of photon and Arduino but I’m just speculating I have no idea! Please help thank you!

I believe A0 is defined as 10 (and you should use A0 rather than 10 when you declare it). Is that the pin you’re using for rcPin?

@Ric Thanks for the reply, I’ve been assigning my pins like you mentioned. The above code is what i used in the arduino IDE and it work superb. I’ve been using the shield shield documentation to cross reference.

Are you using different code for the Photon? If so, you need to post that.

I’ve disconnected my rc remote because it works. I’m trying to pin point the issue by reducing the code. Still no response from my esc. Same code adapted for arduino works flawlessly.

     Servo myservo;
    int limiter = 1500;
    int led = D7;
    
    void setup() {
        
        Serial.begin(9600);
    
        myservo.attach(A4, 700, 2000);
    
        delay(1);
        pinMode(led, OUTPUT);
    }
    
    void loop() {
       digitalWrite(led, HIGH);
       myservo.write(0);
       delay(1000);
       myservo.write(700);
       delay(1000);
       myservo.write(limiter);
      
    }

The led is what I was using to confirm I was obtaining values from my remote between 800-2100. I would have it turn on/off at different values.

I narrowed it down to the arming sequence of the esc for some reason it get initiated differently on the photon then the arduino. More to follow

suggestions?

Since there seem to be differences between the Arduino and the Photon, I think you need to give more information about your setup. Which Arduino board do you have that is working correctly? How exactly is the esc hooked up to the Arduino and Photon? The only thing that I can think of that would be different is the PWM frequency of the different hardware.

PWM frequency is what I’m looking also.

I’ve been using UNO R3.

Setup is pretty straight forward, signal wire of the ESC to a pwm pin of arduino or photon and common ground between esc and the boards.

It’s not clear to me looking online what the frequency of PWM is for the Uno on pin 10. Do you have a way to measure that? The other main difference between the two boards, is that the Uno is operating at 5 volts and the Photon at 3.3.

2 Likes

I’m using a shield shield along with my particle to obtain 5v. It appears that the PWM pins of arduino are slightly different then the photon

This is my current code which works flawlessly with arduino.

        Servo motor;
        
        #define THROTTLE_SIGNAL_IN 0
        #define THROTTLE_SIGNAL_IN_PIN D4
        #define NEUTRAL_THROTTLE 1500
        
        volatile int nThrottleIn = NEUTRAL_THROTTLE;
        volatile unsigned long ulStartPeriod = 0;
        volatile boolean bNewThrottleSignal = false;
        
        int limit = 1550;
        
        void setup() {
          
        attachInterrupt(THROTTLE_SIGNAL_IN, calcInput, CHANGE);
        
        motor.attach(D3);
        
        }
        
        void loop() {
          
          if(bNewThrottleSignal) {
              
            // if(nThrottleIn > limit) {
            //   motor.writeMicroseconds(limit);
            
            // }
            // else {
              motor.writeMicroseconds(nThrottleIn); 
            
            // }
            bNewThrottleSignal = false;
          }
        }
        
        void calcInput() {
          if(digitalRead(THROTTLE_SIGNAL_IN_PIN) == HIGH) {
            ulStartPeriod = micros();
          }
          else {
        
            if(ulStartPeriod && (bNewThrottleSignal == false)) {
              nThrottleIn = (int)(micros() - ulStartPeriod);
              ulStartPeriod = 0;
              bNewThrottleSignal = true;
            }
          }
        }

What are you using the 5 volts on the shield for? If the ESC’s signal wire is hooked directly to a Photon pin, then that is only 3.3 volts. Do you know if that’s ok for your particular device? If not, you might want to put a transistor (maybe an n-channel MOSFET) in between the pin and the ESC, and have the transistor switch 5 volts controlled by the PWM signal from the Photon.

1 Like

I have it connected to the shield and not the photon. Aren’t the pins on the shield shield output 5v

Can someone explain the above command. It does not seem to be in the docs. I assume you are setting the limits.

I have an ESC servo (from a quad copter) which I can only get going at one speed and stutters for other signals. Have tried several ideas. Not really sure if their is an easy method to try different frequencies. I think the default is 50 Hz but ESC use 500 HZ. Any suggestions here?

That version of attach() is described in the Arduino literature.

Parameters

servo: a variable of type Servo

pin: the number of the pin that the servo is attached to

min (optional): the pulse width, in microseconds, corresponding to the minimum (0-degree) angle on the servo (defaults to 544)

max (optional): the pulse width, in microseconds, corresponding to the maximum (180-degree) angle on the servo (defaults to 2400)

@rocksetta, note that this form of attach() is not currently supported on the Particle devices.

1 Like