Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- */
- #include <SPI.h>
- // define the chip select pin for the ADS1118
- const int CS_PIN = 10;
- void setup() {
- // initialize the SPI bus
- SPI.begin();
- SPI.setBitOrder(MSBFIRST);
- SPI.setDataMode(SPI_MODE1);
- SPI.setClockDivider(SPI_CLOCK_DIV16);
- // set the chip select pin as an output and deselect the ADS1118
- pinMode(CS_PIN, OUTPUT);
- digitalWrite(CS_PIN, HIGH);
- // initialize the serial port for debugging
- Serial.begin(9600);
- // print a message to the serial port
- Serial.println("ADS1118 16-Bit AD Converter IC ready!");
- }
- void loop() {
- // select the ADS1118
- digitalWrite(CS_PIN, LOW);
- // send the start conversion command to the ADS1118
- SPI.transfer(0x83);
- SPI.transfer(0x03);
- // wait for the conversion to complete
- delay(10);
- // send the read conversion command to the ADS1118
- SPI.transfer(0x00);
- SPI.transfer(0x00);
- // read the conversion result from the ADS1118
- uint16_t result = SPI.transfer(0x00);
- result = (result << 8) | SPI.transfer(0x00);
- // deselect the ADS1118
- digitalWrite(CS_PIN, HIGH);
- // convert the result to a voltage
- float voltage = (float)result * 6.144 / 32767.0;
- // print the voltage to the serial port
- Serial.print("Voltage: ");
- Serial.print(voltage, 3);
- Serial.println("V");
- // wait for a short time
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement