Servo shaking when LIDAR added

Hello,

My project is to build à LIDAR scanner.

First, I wrote 2 programs independently to move my servo and to get the distance the LIDAR is measuring.

Now I try to put these programs together but when I execute the code below, the servo doesn't work like it does when I run it alone : it shakes, and doesn't initalize correctly.

What can I do ?

Thanks for your help

#include <Servo.h>
#include <SoftwareSerial.h>

Servo myservo;
SoftwareSerial Serial1(3,2); 


const int servoAnalogOut=A1; 
unsigned int servoValue0Deg, servoValue180Deg; 
int pos = 0;   



int dist;
int strength;
float temprature;
int check; 
int i;
int uart[9]; 
const int HEADER=0x59; 


void setup() {


  myservo.attach(9);
  Serial.begin(115200); 
  Serial1.begin(115200); 

}
void loop() {

  for (int i=0; i <= 180; i++)
  {
    myservo.write(i);
    delay(100);
    Serial.println(map(analogRead(servoAnalogOut),servoValue0Deg,servoValue180Deg, 0, 180));

    if (Serial1.available()) 
 
 {  //check if serial port has data input

  if(Serial1.read() == HEADER) 
  
  { 
  uart[0]=HEADER;

  
    if (Serial1.read() == HEADER) 
    
    {
      uart[1] = HEADER;
      for (i = 2; i < 9; i++) 
      { 
        uart[i] = Serial1.read();
      }

      check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
      if (uart[8] == (check & 0xff))
      
      { 
      
      dist = uart[2] + uart[3] * 256; 
      strength = uart[4] + uart[5] * 256; 
      temprature = uart[6] + uart[7] *256;
      temprature = temprature/8 - 256;
      Serial.print("dist = ");
      Serial.print(dist); 
      Serial.print('\t');
      Serial.print("strength = ");
      Serial.print(strength); 
      Serial.print("\t Chip Temprature = ");
      Serial.print(temprature);
      Serial.println(" celcius degree"); 
      }
    }
 }
 }

    
void calibration(){
  myservo.write(0); 
  delay(2000); 
  servoValue0Deg= analogRead(servoAnalogOut); 
  Serial.println("Pot value for 0 deg is " + String(servoValue0Deg)); 
  delay(500); 
  myservo.write(180); 
  delay(2000); 
  servoValue180Deg= analogRead(servoAnalogOut); 
  Serial.println("Pot value for 180 deg is " + String(servoValue0Deg));
  delay(500); 
  Serial.println("Now going to 90 Degrees");
  myservo.write(90);
  delay(2000);
  ]
 

  
}
}
}

The servo library and software serial libraries conflict over timer usage. Try the servotimer2 library.

Thanks for your answer, I now use the servotimer2 library so I had to rebuild my initialization functions and a few things: it's now operating but alone only...

My problem is that in the final code below, the sensor parameters aren't displayed so I assume one of the if(...) isn't verified and I think this is because of the servo.

For the servo I also use as you can see an analog in to measure the potenial : any chance this conflicts with the sensor ?

The other problem is that the servo is moving to the position I ask him to go but when he reaches this position he shakes by like -3 to +3 degres.

Do you have a solution ?

#include <ServoTimer2.h>
#include <SoftwareSerial.h>

ServoTimer2 servo1;
SoftwareSerial Serial1(3,2); 

const int servoAnalogOut=A5;
unsigned int servoValue0Deg, servoValue160Deg;
int pos=550;

int dist; 
int strength; 
float temprature;
int check; 
int i;
int uart[9];
const int HEADER=0x59;


void setup()  
{ 
  servo1.attach(9);    
  Serial.begin(115200);
  Serial1.begin(115200);
  calibration();

}
 
void loop()  
{          
  delay(1000);
  servo1.write(pos);
  delay(1000);

    if (Serial1.available()) {  

      if(Serial1.read() == HEADER) { 
        uart[0]=HEADER;

    
        if (Serial1.read() == HEADER) { 
          uart[1] = HEADER;
          for (i = 2; i < 9; i++) 
          { //save data in array
            uart[i] = Serial1.read();
          }

          check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
          if (uart[8] == (check & 0xff)) { 
        
            dist = uart[2] + uart[3] * 256; 
            strength = uart[4] + uart[5] * 256; 
            temprature = uart[6] + uart[7] *256;
            temprature = temprature/8 - 256;
            Serial.print("dist = ");
            Serial.print(dist);
            Serial.print('\t');
            Serial.print("strength = ");
            Serial.print(strength); 
            Serial.print("\t Chip Temprature = ");
            Serial.print(temprature);
            Serial.println(" celcius degree"); 
          }
        }
      }
    }
  delay(1000);
  Serial.println(map(analogRead(servoAnalogOut),servoValue0Deg,servoValue160Deg, 0, 160));
  pos+=13;  
   

} 


void calibration(){
  servo1.write(550);
  delay(2000);
  servoValue0Deg= analogRead(servoAnalogOut);
  Serial.println("Pot value for 0 deg is " + String(servoValue0Deg));
  delay(500);

  servo1.write(2500);
  delay(2000);
  servoValue160Deg= analogRead(servoAnalogOut);
  Serial.println("Pot value for 180 deg is " + String(servoValue160Deg));
  delay(500);

}

Put in Serial.print() statements to test that assumption.

How is the servo powered? Please post a schematic.

Here it is

Also join a global picture of the circuit

Schematic.pdf (194,0 Ko)

I checked it, the program doesn't enter the 3rd if(...) of the loop.

I also join a link to a video of it "working" and a picture of the result I get in the serial monitor.

Hi,
How are you powering your project?

Do you have a DMM?
If so, measure the voltage supply at the servo plug, it sounds like you need more current for the servo.
It is best if you can post your schematics into your post, export as jpg.

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

Hi,

My project has not any other power source than the one provided by the USB connexion between the arduino and my PC.

I don't have any DMM :frowning: but I can look for it next week.

Sorry for the pdf, I will directly export as jpg next time.

Do you think I need to add a power source ?
If yes please tell me what to use (I'm a arduino beginner).

Yes, the servo needs more current that the USB safely provide.

Make sure to connect the external power source ground to Aeduino ground as well as servo ground as shown.

Thanks ! I will get something to power it up correctly.

So the failure comes from the fact that the current needed is to high when servo + sensor are plugged ? Because one at a time, it works.

Yes. A 4 AA cell battery pack is a good option for servo power.

So I plugged a 5V 3amps alim on the arduino and still the same problem: the sensor doesn't enter the 3rd if(...) of the loop and the servo is shaking even with 0 instruction of movement given.

Please post a schematic and clear photos of your wiring.

Is the external supply ground connected to Arduino ground.

To what pins of the UNO did you connect the 5V 3A "alim" ???
What is a "alim"?

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

Nothing really changed about the wiring.

I added a power supply from the AC outlet using the wire you can see on the pictures below which goes directly in the arduino outlet.

This new power source isn't represented on the schematic and neither is the usb cable (you can see both in the picture).

Here are the new schematic and a clear picture of my wiring:



What is the voltage/current rating of that supply.

Powering the setup through the DC socket is likely a far worse solution.
Now everything runs through the 5volt regulator.
Leo..

Your external supply is connected to the power plug on the Uno. That feeds the weak onboard 5V regulator. The 5V regulator cannot supply sufficient current for the servo so the servo twitches. The servo's motor pulls stall current every time that it starts and it starts a lot while trying to maintain position. Connect the servo to that power supply as shown in post #11 so the the external supply is powering the servo, not the puny on board regulator.

Is there a way to connect an external supply to that shield so that the shield supplies the servos? Can you post a data sheet, schematic or technical documentation on the sheild?

If the USB lead was still plugged in, then the power would still have come from USB.
The DC socket needs at least 7volt for the Uno to change from USB to DC socket supply.

That V1.2 shield has no option for external power.
According to the Eagle files I found, all white sockets are powered from the Uo's 5volt rail.
Leo..