Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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:
- 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.
- 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.
- 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
- */
- const int SONAR_PIN = A0; // MB1000 sensor connected to analog pin A0
- // Sensor parameters
- const float VOLTAGE_REF = 5.0;
- const float VOLTAGE_TO_DISTANCE = 512.0; // 1V per 512 units (1 inch or 1 cm)
- void setup() {
- Serial.begin(9600);
- }
- void loop() {
- int sensorValue = analogRead(SONAR_PIN);
- // Convert the analog reading to voltage
- float voltage = sensorValue * (VOLTAGE_REF / 1023.0);
- // Calculate the distance
- float distance = voltage * VOLTAGE_TO_DISTANCE;
- Serial.print("Sensor Value: ");
- Serial.println(sensorValue);
- Serial.print("Voltage: ");
- Serial.print(voltage);
- Serial.println(" V");
- Serial.print("Distance: ");
- Serial.print(distance);
- Serial.println(" units");
- delay(500); // Wait 500ms between readings
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement