Advertisement
microrobotics

Maxbotix LV-MaxSonar-EZ0 Sonar Range Finder MB1000

Mar 31st, 2023
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Maxbotix LV-MaxSonar-EZ0 (MB1000) is an ultrasonic range finder that can output distance data using an analog voltage, a pulse width, or a serial data output. In this example, we'll use the analog voltage output to read the sensor's data using an Arduino:
  3.  
  4. To wire the MB1000 sensor, connect its AN (analog output) pin to the SONAR_PIN (analog pin A0) on the Arduino. Connect the +5V pin to the 5V pin on the Arduino and the GND pin to the ground.
  5.  
  6. This code will continuously read the sensor's output and print the raw readings, the corresponding voltage, and the calculated distance on the Serial Monitor. The distance will be in the units (inches or centimeters) configured for the sensor. Make sure to adjust the VOLTAGE_TO_DISTANCE constant according to your specific sensor configuration.
  7.  
  8. If you prefer to use the pulse width or serial data output instead of the analog voltage output, you can find example code for those modes in the Maxbotix LV-MaxSonar-EZ datasheet: https://www.maxbotix.com/documents/LV-MaxSonar-EZ_Datasheet.pdf
  9. */
  10.  
  11. const int SONAR_PIN = A0; // MB1000 sensor connected to analog pin A0
  12.  
  13. // Sensor parameters
  14. const float VOLTAGE_REF = 5.0;
  15. const float VOLTAGE_TO_DISTANCE = 512.0; // 1V per 512 units (1 inch or 1 cm)
  16.  
  17. void setup() {
  18.   Serial.begin(9600);
  19. }
  20.  
  21. void loop() {
  22.   int sensorValue = analogRead(SONAR_PIN);
  23.  
  24.   // Convert the analog reading to voltage
  25.   float voltage = sensorValue * (VOLTAGE_REF / 1023.0);
  26.  
  27.   // Calculate the distance
  28.   float distance = voltage * VOLTAGE_TO_DISTANCE;
  29.  
  30.   Serial.print("Sensor Value: ");
  31.   Serial.println(sensorValue);
  32.   Serial.print("Voltage: ");
  33.   Serial.print(voltage);
  34.   Serial.println(" V");
  35.   Serial.print("Distance: ");
  36.   Serial.print(distance);
  37.   Serial.println(" units");
  38.  
  39.   delay(500); // Wait 500ms between readings
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement