Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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.comtest-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://digilent.com/shop/digital-discovery-portable-usb-logic-analyzer-and-digital-pattern-generator/|Digital Discovery]]
 +  * [[https://digilent.com/shop/software/digilent-waveforms/|WaveForms Software]]
 +  * A computer with a USB port to run the software
 +  * [[https://www.python.org/downloads/Python|Python Software]] with Python 2.7 interpreter and matplotlib
 +  * An accelerometer with an I2C interface
 +    * //In this example we use the {{ :learn:instrumentation:tutorials:interfacing-i2c-dd-python:lis35de.pdf |LIS35DE}} mounted on a {{ :learn:instrumentation:tutorials:interfacing-i2c-dd-python:kamodmems2.pdf |KAmodMEMS2 module}} //
 +  * 2x6 MTE Cables may be used for the connections
 +  * {{ :learn:instrumentation:tutorials:interfacing-i2c-dd-python:lis35de.zip |Python Source Code}} downloaded to the computer
 +
 +----
 +===== Step 1: Hardware Setup  =====
 +<WRAP group> <WRAP column half>
 +1. Connect GND and Vdd of the sensor to the  Digital Discovery's GND and VIO pins, respectively. 
 +
 +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 .
 +</WRAP> <WRAP column half>
 +{{:learn:instrumentation:tutorials:interfacing-i2c-dd-python:DD_1.jpg?400|}}
 +</WRAP> </WRAP>
 +
 +----
 +===== 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/output (such as for the I2C interface) start counting channels from DIO24. DIO24 will be referenced as 0, DIO 25 as 1, etc.//
 +
 +<code>
 +# set I2C frequency and pins
 +dwf.FDwfDigitalI2cRateSet(hdwf, c_double(1e5)) # 100kHz
 +dwf.FDwfDigitalI2cSclSet(hdwf, c_int(32-24)) # SCL = DIO-32
 +dwf.FDwfDigitalI2cSdaSet(hdwf, c_int(33-24)) # SDA = DIO-33
 +</code>
 +</WRAP>
 +
 +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.
 +
 +<code>
 +dwf.FDwfDigitalI2cWriteRead(hdwf, c_int(address << 1), (c_ubyte*1)(LIS35DEreg.REG_OUTX | LIS35DEreg.READ_INC), c_int(1), rgRX, c_int(5), byref(iNak))
 +</code>
 +</WRAP>
 +
 +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  the asterisk is in the center – it behaves as a spirit level.
 +
 +After closing the window the script will shut down the sensor and disconnect from the Digital Discovery.
 +</WRAP> <WRAP column half>
 +{{:learn:instrumentation:tutorials:interfacing-i2c-dd-python:DD_2.jpg?200|}}
 +</WRAP> </WRAP>
 +----
 +===== 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's [[test-and-measurement/digital-discovery/start|Resource Center]].
 +For more information on WaveForms visit the [[software:waveforms:waveforms-3:reference-manual|WaveForms Reference Manual]]. 
 +For technical support, please visit the [[https://forum.digilentinc.com/forum/8-test-and-measurement/|Test and Measurement]] section of the Digilent Forums.
 +
 +----
 +
 +{{tag>learn instrumentation project digital-discovery }}