Advertisement
microrobotics

DS1115 A/D Converter sample code

May 6th, 2023
1,495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2.  
  3. const int DS1115_Address = 0x48;  // I2C address of DS1115
  4. float voltageValue = 0.0;
  5.  
  6. void setup() {
  7.   Serial.begin(9600);
  8.   Wire.begin();
  9. }
  10.  
  11. void loop() {
  12.   int rawValue;
  13.   Wire.beginTransmission(DS1115_Address);
  14.   Wire.write(0x00); // address of conversion register
  15.   Wire.endTransmission();
  16.   delay(10);  // conversion time for DS1115
  17.   Wire.requestFrom(DS1115_Address, 2);
  18.   if (Wire.available() == 2) {
  19.     rawValue = Wire.read() << 8 | Wire.read();
  20.     voltageValue = rawValue * 5.0 / 32768.0; // 5V reference voltage, 16-bit resolution
  21.     Serial.print("Voltage: ");
  22.     Serial.print(voltageValue);
  23.     Serial.println("V");
  24.   }
  25.   delay(1000);
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement