Advertisement
microrobotics

Maxbotix XL-MaxSonar-AE0 Sonar Range Finder MB1300

Mar 29th, 2023 (edited)
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.41 KB | None | 0 0
  1. /*
  2. In this example code, the sonar range finder is connected to analog input pin A0 and digital output pin 2 on the Arduino. The setup() function sets the digital pin to enable the sonar range finder and initializes serial communication. The loop() function reads the analog value from the sonar range finder, converts it to inches, and prints the range to the serial monitor. A short delay is added before taking another reading to prevent the sonar range finder from being read too frequently. Note that the conversion factor from analog value to inches may need to be adjusted depending on the specific model of the sonar range finder being used.
  3. */
  4. const int PIN_ANALOG_OUT = A0; // Analog output pin of the sonar range finder
  5. const int PIN_ENABLE = 2; // Digital pin to enable the sonar range finder
  6.  
  7. void setup() {
  8.   pinMode(PIN_ENABLE, OUTPUT); // Set the enable pin as an output
  9.   digitalWrite(PIN_ENABLE, HIGH); // Enable the sonar range finder
  10.   Serial.begin(9600); // Initialize serial communication at 9600 baud
  11. }
  12.  
  13. void loop() {
  14.   int sensorValue = analogRead(PIN_ANALOG_OUT); // Read the analog value from the sonar range finder
  15.   float rangeInches = sensorValue * 0.0098; // Convert the analog value to inches
  16.   Serial.print("Range: "); // Print the range to the serial monitor
  17.   Serial.print(rangeInches);
  18.   Serial.println(" inches");
  19.   delay(100); // Wait for a short time before taking another reading
  20. }
  21.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement