Problem with controlling a solar tracker

Hello the Cayenne community. I’m new to Cayenne and I’m working on a small solar tracker project with Arduino UNO and a W5100 Ethernet shield.

the idea being to be able to see in real terms the data provided by my photoresist light sensors and my small solar panel.

my follower must operate in automatic mode via the data coming from the photoresist sensors and also in manual mode where I want to control my two servomotors by their associated widgets.

the two modes are conditioned by the lighting of an LED which is associated with a button on cayenne. when the LED is on the tracker is in automatic mode and tracking the position of the light itself, and when it is off the tracker is in manual mode.

the problem being that, when I switch to manual mode, the control of two servomotors is successful, but when I switch to automatic mode, the follower hardly works because the servos move little by little apart from one second.
And when I comment Cayenne.begin and Cayenne.loop to disconnect me from Cayenne, the automatic mode works very well by following the light. I need your help on this! Note that my two servos are plugged into the arduino PWM port.

here is the arduino code:

#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>    
#include <Servo.h>                  
//MQTT credentials   
char username[]="47739f30-1e41-11ec-8da3-474359af83d7";
char password[]="45c57473905ac357a70d4368491d2f4df0525590";
char clientID[]="8f654910-1e41-11ec-9f5b-45181495093e";

Servo servo_x;                   //up-down servomotor  
int servoh = 0;
int servohLimitHigh = 170;     
int servohLimitLow = 10;       

Servo servo_z;                   //left-right servomotor 
int servov = 0; 
int servovLimitHigh = 170;
int servovLimitLow = 10;

int topl,topr,botl,botr;
int threshold_value=10;        
int Vout;

void setup()
  Cayenne.begin(username, password, clientID);


void loop()
{ topr= analogRead(A2);       
  topl= analogRead(A3);         
  botl= analogRead(A4);         
  botr= analogRead(A5);        
  Vout=(analogRead(A1) * 5.0) / 1023;
  if (digitalRead(3)==LOW){
    Serial.println(" Manual-mode");
  if (digitalRead(3)==HIGH){
    Serial.println(" Automatic-mode");
    servoh =;
    servov =;
    int avgtop = (topr + topl) / 2;     
    int avgbot = (botr + botl) / 2;   
    int avgright = (topr + botr) / 2;   
    int avgleft = (topl + botl) / 2;    
    int diffhori= avgtop - avgbot;      
    int diffverti= avgleft - avgright;    
    /*tracking according to horizontal axis*/ 
    if (abs(diffhori) <= threshold_value)
     servo_x.write(servoh);            //stop the servo up-down
    }else {
       if (diffhori > threshold_value)
          { Serial.println(" x - 2 ");
          servo_x.write(servoh -2);    //Clockwise rotation CW
          if (servoh > servohLimitHigh)
           servoh = servohLimitHigh;
          }else {
           servo_x.write(servoh +2);   //CCW
           if (servoh < servohLimitLow)
           servoh = servohLimitLow;
    /*tracking according to vertical axis*/ 
    if (abs(diffverti) <= threshold_value)
     servo_z.write(servov);       //stop the servo left-right
       if (diffverti > threshold_value)
       servo_z.write(servov -2);  //CW
       if (servov > servovLimitHigh) 
       servov = servovLimitHigh;
        servo_z.write(servov +2);  //CCW
        if (servov < servovLimitLow) 
        servov = servovLimitLow;
// Cayenne Functions
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", 8, 3, value);
CAYENNE_IN(7){ //up-down servo motor
  if (digitalRead(3)==HIGH){ //Automatic_modez
  else{ //Manual_mode
  servo_x.write(getValue.asDouble() * 180);
CAYENNE_IN(6){ //left-right servo motor
  if (digitalRead(3)==HIGH){
  servo_z.write(getValue.asDouble() * 180);

CAYENNE_OUT(0) { //Current
  int current = Vout/10;
  Cayenne.virtualWrite(0, current);
  Serial.print("Current: ");
CAYENNE_OUT(1) { //Voltage
  int voltage = Vout * 2;
  Cayenne.virtualWrite(1, voltage);
  Serial.print("Voltage: ");
CAYENNE_OUT(2){ //LDR Top-right
  Cayenne.virtualWrite(2, topr);
CAYENNE_OUT(3){ //LDR Top-left
CAYENNE_OUT(4){ //LDR Bot-left
CAYENNE_OUT(5){ //LDR Bot-right

you can try passing a value, using Cayenne.loop(60), where 60 is the time in second so that it runs at a larger interval.

1 Like

Merci bien. mon problème a été résolu

ohhhh, my life saver thank you !.. :heart_eyes:

1 Like