Example Code for Arduino-Voltage Analog Output
This project demonstrates how to read voltage output from the SEN0301 Water-proof Ultrasonic Sensor in voltage analog mode using an Arduino board. Users will learn how to convert the analog voltage signal to distance.
Hardware Preparation
- DFRduino UNO R3 (or similar) x 1
- Multi USB/RS232/RS485/TTL Converter V2.0 x 1
- Water-proof Ultrasonic Sensor (ULA) x 1
- Connectors
Software Preparation
Wiring Diagram
The internal probe surface is regarded as the detecting starting point by default. If you take the plane of the bell-mouth as starting point, then the distance will be the detected distance minus 40mm.
Sample Code
* ****************************************************
* @brief Water-proof Ultrasonic Sensor (ULS)
* @copyright [DFRobot](https://www.dfrobot.com), 2016
* @copyright GNU Lesser General Public License
* @author [Xiaoyu]([email protected])
* @version V1.0
* @date 2019-03-11
* GNU Lesser General Public License.
* All above must be included in any redistribution
* ****************************************************/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int analogPin = 0;//Define analog pin 0
int val = 0;
void setup() {
Serial.begin(57600); // Enable serial port and set band rate to 57600 bps
mySerial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
val = analogRead(analogPin);// Read analog port value
float voltage= val* (3.3/1023);//Convert data
Serial.print("Vout:");
Serial.print(voltage);
Serial.println("m");
delay(200);
}
Result
Was this article helpful?
