Fail to read data from an rplidar when powering via vin (works well on usb))

Hello,
I'm reading value from a rplidar A1M8 to send data from an arduino mega to an esp32 and i have some issues when the arduino is powered via vin.
My alimentation is 12v battery with a buck converter (max 3A)

Here's my code:

#include <RPLidar.h>

#include <ezButton.h>

ezButton StartSwitch(53);
ezButton ElevatorSwitchFront(52);
ezButton ElevatorSwitchBack(51);

#define RPLIDAR_MOTOR 3 // The PWM pin for control the speed of RPLIDAR's motor (MOTOCTRL).
#define USE_LIDAR true

RPLidar lidar;
                      
void setup() {
  Serial.begin(115200);
  Serial2.begin(115200);
  Serial3.begin(115200);  // For RPLidar
  lidar.begin(Serial3);
  pinMode(RPLIDAR_MOTOR, OUTPUT);  // set pin modes

  StartSwitch.setDebounceTime(50);
  ElevatorSwitchFront.setDebounceTime(50);
  ElevatorSwitchBack.setDebounceTime(50);
}

float minDistance = 100000;
float angleAtMinDist = 0;

void loop() {
  StartSwitch.loop();
  ElevatorSwitchFront.loop();
  ElevatorSwitchBack.loop();

  int startState = StartSwitch.getState();
  int frontElevatorState = ElevatorSwitchFront.getState();
  int backElevatorState = ElevatorSwitchBack.getState();

  if(USE_LIDAR){
    if (IS_OK(lidar.waitPoint())) {
        //perform data processing here... 
        float distance = lidar.getCurrentPoint().distance;
        float angle = lidar.getCurrentPoint().angle;  // 0-360 deg
        
        if (lidar.getCurrentPoint().startBit ) {
          // a new scan, display the previous data...
          Serial.print("lidar:");
          Serial.print(minDistance);
          Serial.print("/");
          Serial.println(angleAtMinDist);
          Serial2.print("lidar:");
          Serial2.print(minDistance);
          Serial2.print("/");
          Serial2.println(angleAtMinDist);
          Serial.print("switch:");
          Serial.print(startState);
          Serial.print("/");
          Serial.print(frontElevatorState);
          Serial.print("/");
          Serial.println(backElevatorState);
          Serial2.print("switch:");
          Serial2.print(startState);
          Serial2.print("/");
          Serial2.print(frontElevatorState);
          Serial2.print("/");
          Serial2.println(backElevatorState);
            
          angleAtMinDist = 0;
          
          minDistance = 100000;
        } else {
          
          if ( distance > 0 &&  distance < minDistance) {
            
              minDistance = distance;
              angleAtMinDist = angle;

              
          }
        }
      }
      else {
        analogWrite(RPLIDAR_MOTOR, 0); //stop the rplidar motor
        // Try to detect RPLIDAR
        rplidar_response_device_info_t info;
        if (IS_OK(lidar.getDeviceInfo(info, 100))) {
          // Detected
          lidar.startScan();
          analogWrite(RPLIDAR_MOTOR, 255);
          delay(3000);
        }
      }
  }
  else{
    Serial.print("switch:");
    Serial.print(startState);
    Serial.print("/");
    Serial.print(frontElevatorState);
    Serial.print("/");
    Serial.println(backElevatorState);
    Serial2.print("switch:");
    Serial2.print(startState);
    Serial2.print("/");
    Serial2.print(frontElevatorState);
    Serial2.print("/");
    Serial2.println(backElevatorState);
    delay(150);
  }
  
  
}

(I'm also sending data from limit switchES)

-The code works very well when the arduino is powered via usb and I retrieve all the data I need

-The code does not work when powered by vin (I checked with a multimeter and it's 5V) , The motor of the lidar does not start

-when USE_LIDAR is at false it works well and I retrieve my limit switch states on the esp

-If the usb is connected and i powered the arduino via vin and then i unplugged the usb port, the esp still retrieve the data.

If you have any idea how to have this working without plugging the arduino each time i would really appreciate it

Rodolphe

What voltage.
Vin of a Mega expects 6.5volt minimum, and max voltage depends on how much current you are drawing from the Mega. If you have a 5volt supply, then connect it to the 5volt pin. Make sure you disconnect everything if you want to reconnect to USB.
Leo..

1 Like

Thank you for the reply.
I always assumed the vin works at 5v and i didn't even bother to look it up. Terrible mistake
Arduino recommend 7-12V on the vin so i set my buck converter to 7V and it works.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.