Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| test-and-measurement:digital-discovery:demos:interfacing-i2c-and-python [2022/09/12 15:51] – changed forum.digilentinc.com to forum.digilent.com jeff.greenberg_ni.com | test-and-measurement:digital-discovery:demos:interfacing-i2c-and-python [2022/10/04 11:38] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Using a Python Script to Interface the Digital Discovery with an I2C Device | ||
| + | ~~TechArticle~~ | ||
| + | |||
| + | In this tutorial we will use a python script to read and graphically present data from a digital accelerometer with an I2C interface (LIS35DE). | ||
| + | ---- | ||
| + | |||
| + | ==== Inventory ==== | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * A computer with a USB port to run the software | ||
| + | * [[https:// | ||
| + | * An accelerometer with an I2C interface | ||
| + | * //In this example we use the {{ : | ||
| + | * 2x6 MTE Cables may be used for the connections | ||
| + | * {{ : | ||
| + | |||
| + | ---- | ||
| + | ===== Step 1: Hardware Setup ===== | ||
| + | <WRAP group> <WRAP column half> | ||
| + | 1. Connect GND and Vdd of the sensor to the Digital Discovery' | ||
| + | |||
| + | 2. The sensor is dual interface, SPI or I2C. It should remain in I2C mode. The CS pin of the device need to be connected to Vdd. | ||
| + | |||
| + | 3. The chip has a selectable I2C address; to select 0x38 for the address, the SDO pin needs to be tied to GND. | ||
| + | |||
| + | 4. SDA and SCL lines should be connected to DIO33 and DIO32, respectively. | ||
| + | |||
| + | 5. These lines should have suitable pull-up resistors connected – resistors are already mounted on the selected module . | ||
| + | </ | ||
| + | {{: | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | ===== Step 2: Script | ||
| + | |||
| + | The downloaded Python script will: | ||
| + | |||
| + | 1. Load WaveForms SDK | ||
| + | |||
| + | 2. Connect to the Digital Discovery | ||
| + | |||
| + | 3. Enable the 3.3V power supply for the sensor | ||
| + | |||
| + | 4. Configure the I2C interface | ||
| + | |||
| + | <WRAP indent> | ||
| + | To configure the demo, the user needs to set the operating frequency of the bus and select which pins of the Digital Discovery should be used. | ||
| + | |||
| + | **Note**: //The SDK functions related to digital output, or input/ | ||
| + | |||
| + | < | ||
| + | # set I2C frequency and pins | ||
| + | dwf.FDwfDigitalI2cRateSet(hdwf, | ||
| + | dwf.FDwfDigitalI2cSclSet(hdwf, | ||
| + | dwf.FDwfDigitalI2cSdaSet(hdwf, | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | 5. Initialize the sensor and display | ||
| + | |||
| + | 6. Read sensor data and update graphs as long as the plot window is open | ||
| + | |||
| + | <WRAP indent> | ||
| + | Each read operation from the sensor is a write-read operation on the I2C bus. A device address is set for a complete operation and WaveForms SDK automatically maintains the R/W bit. Two arrays are used to write and read data, and user specifies how many bytes should be written, and , after repeated start, read. In our case only one byte is written. The register address is combined with a bit mask indicating that the sensor should increment the register address after each byte is read from the bus. Then, 5 bytes are read from the subsequent registers beginning at the OUTX register. The read data contains 3 bytes with X, Y and Z acceleration data represented in two’s complement and two dummy bytes. | ||
| + | |||
| + | < | ||
| + | dwf.FDwfDigitalI2cWriteRead(hdwf, | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | 7. After closing the plot window, disable the power supply and disconnect from the device | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Step 3: Running the Demo ===== | ||
| + | <WRAP group> <WRAP column half> | ||
| + | Before running the script make sure that all the connections are correct, and place the sensor on a flat surface for a “virtual spirit level” calibration. When ready, run the script. The following window should appear. | ||
| + | |||
| + | The upper part of the window presents live XYZ acceleration from the sensor as a function of time, while the lower part presents the actual X and Y acceleration as a blue asterisk (*) – when the sensor is in the same orientation as at the script start-up | ||
| + | |||
| + | After closing the window the script will shut down the sensor and disconnect from the Digital Discovery. | ||
| + | </ | ||
| + | {{: | ||
| + | </ | ||
| + | ---- | ||
| + | ===== Step 4: Wrapping Up ===== | ||
| + | |||
| + | Using a Digital Discovery and WaveForms SDK it is possible to communicate with a representative digital sensor. With a little effort, communication and configuration may be tested, as well as a simple graphical interface be presented. Some automatic testing routines may be implemented for design control. | ||
| + | ---- | ||
| + | ===== Next Steps ===== | ||
| + | |||
| + | For more guides on how to use the Digital Discovery, return to the device' | ||
| + | For more information on WaveForms visit the [[software: | ||
| + | For technical support, please visit the [[https:// | ||
| + | |||
| + | ---- | ||
| + | |||
| + | {{tag> | ||

