Advertisement
microrobotics

ADS1118 16-Bit AD Converter IC

Apr 17th, 2023
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This code uses the SPI interface to communicate with the ADS1118. In the setup() function, the SPI bus is initialized with the SPI.begin() function and the chip select pin is set as an output using the pinMode() function.
  3.  
  4. In the loop() function, the ADS1118 is selected using the digitalWrite() function, and the start conversion command is sent using the SPI.transfer() function. A short delay is added to allow the conversion to complete.
  5.  
  6. The read conversion command is then sent using SPI.transfer(), and the conversion result is read from the ADS1118 using two SPI.transfer() functions. The result is combined into a 16-bit integer using bitwise operations.
  7.  
  8. The ADS1118 is then deselected using digitalWrite(), and the conversion result is converted to a voltage using the formula voltage = result * 6.144 / 32767, where 6.144V is the full-scale range of the ADS1118 and 32767 is the maximum value of a 16-bit integer.
  9.  
  10. The voltage is printed to the serial port using the Serial.print() function. A short delay is added using the delay() function before the loop starts again.
  11.  
  12. This code provides a basic framework for interfacing with the ADS1118 16-Bit AD Converter IC using an Arduino. You can modify the code to suit your specific requirements, such as changing the conversion range or adding more channels.
  13. */
  14.  
  15. #include <SPI.h>
  16.  
  17. // define the chip select pin for the ADS1118
  18. const int CS_PIN = 10;
  19.  
  20. void setup() {
  21.   // initialize the SPI bus
  22.   SPI.begin();
  23.   SPI.setBitOrder(MSBFIRST);
  24.   SPI.setDataMode(SPI_MODE1);
  25.   SPI.setClockDivider(SPI_CLOCK_DIV16);
  26.  
  27.   // set the chip select pin as an output and deselect the ADS1118
  28.   pinMode(CS_PIN, OUTPUT);
  29.   digitalWrite(CS_PIN, HIGH);
  30.  
  31.   // initialize the serial port for debugging
  32.   Serial.begin(9600);
  33.  
  34.   // print a message to the serial port
  35.   Serial.println("ADS1118 16-Bit AD Converter IC ready!");
  36. }
  37.  
  38. void loop() {
  39.   // select the ADS1118
  40.   digitalWrite(CS_PIN, LOW);
  41.  
  42.   // send the start conversion command to the ADS1118
  43.   SPI.transfer(0x83);
  44.   SPI.transfer(0x03);
  45.  
  46.   // wait for the conversion to complete
  47.   delay(10);
  48.  
  49.   // send the read conversion command to the ADS1118
  50.   SPI.transfer(0x00);
  51.   SPI.transfer(0x00);
  52.  
  53.   // read the conversion result from the ADS1118
  54.   uint16_t result = SPI.transfer(0x00);
  55.   result = (result << 8) | SPI.transfer(0x00);
  56.  
  57.   // deselect the ADS1118
  58.   digitalWrite(CS_PIN, HIGH);
  59.  
  60.   // convert the result to a voltage
  61.   float voltage = (float)result * 6.144 / 32767.0;
  62.  
  63.   // print the voltage to the serial port
  64.   Serial.print("Voltage: ");
  65.   Serial.print(voltage, 3);
  66.   Serial.println("V");
  67.  
  68.   // wait for a short time
  69.   delay(100);
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement