KAmodRPi ADC DAC: Difference between revisions
From Kamamilabs.com - Wiki
Anna Kubacka (talk | contribs) No edit summary |
Anna Kubacka (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
__jzpdf__ | |||
====== Module description ====== | ====== Module description ====== | ||
[https://kamami.pl/moduly-rozszerzajace-raspberry-pi-2/234072-kamodrpi-adc-dac-modul-przetwornika-ac-i-ca-dla-komputerow-raspberry-pi2-raspberry-pi-i-raspberry-pi-mcp3021-mcp4716.html KAmodRPi ADC_DAC module] features 10-bit analog to digital converter (ADC) MCP3021 and 10-bit digital to analog converter (DAC) MCP4716. Module connector fits all versions of Raspberry Pi. You can interface MCP3021 and MCP4716 with I2C. ADC input voltage range can be configured to 0-3.3V, 0-5V and 0-10V with a jumper. | [https://kamami.pl/moduly-rozszerzajace-raspberry-pi-2/234072-kamodrpi-adc-dac-modul-przetwornika-ac-i-ca-dla-komputerow-raspberry-pi2-raspberry-pi-i-raspberry-pi-mcp3021-mcp4716.html KAmodRPi ADC_DAC module] features 10-bit analog to digital converter (ADC) MCP3021 and 10-bit digital to analog converter (DAC) MCP4716. Module connector fits all versions of Raspberry Pi. You can interface MCP3021 and MCP4716 with I2C. ADC input voltage range can be configured to 0-3.3V, 0-5V and 0-10V with a jumper. |
Latest revision as of 11:56, 23 September 2020
Module description
KAmodRPi ADC_DAC module features 10-bit analog to digital converter (ADC) MCP3021 and 10-bit digital to analog converter (DAC) MCP4716. Module connector fits all versions of Raspberry Pi. You can interface MCP3021 and MCP4716 with I2C. ADC input voltage range can be configured to 0-3.3V, 0-5V and 0-10V with a jumper. Sample program will show how to use the module with Raspberry Pi using Python.
Prerequisites
To run sample program you need
- Raspberry Pi (any version)
- SD card with Raspbian OS
- KAmodRPi ADC_DAC module
- One jumper connector or wire
Hardware setup
- Connect KAmodRPi ADC_DAC module to Raspberry Pi
- Connect pins VIN and VOU with jumper or wire
Configuration
First of all we need to enable I2C support in Raspbian, run:
sudo raspi-config
Select //Advanced Options//, //I2C//, then select //Yes// twice, then reboot Raspberry Pi. Now install i2c-tools:
sudo apt-get install i2c-tools
Let's check if Raspberry Pi can communicate with module:
i2cdetect -y 1
Output should look like this:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- 4d -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
We can see that Raspberry Pi found two I2C devices:
- MCP3021 - address 0x4d
- MCP4716 - address 0x60
Python code
To be able to use I2C in Python we need to install smbus library:
sudo apt-get install python-smbus
Now you can use simple library, put the code below into file KAmodRPiADCDAC.py:
import smbus
class KAmodRPiADCDAC:
VINmax = 10
bus = smbus.SMBus(1)
adAddress = 0x4d
daAddress = 0x60
def setVINmax(self, v):
self.VINmax = v
def readMCP3021(self):
rd = self.bus.read_word_data(self.adAddress, 0)
return ((rd & 0xFF) << 8) | ((rd & 0xFF00) >> 8)
def readMCP3021Voltage(self):
val = self.readMCP3021() >> 2
return float(self.VINmax) * float(val) / float(0x3FF)
def writeMCP4716(self, level):
self.bus.write_i2c_block_data(self.daAddress, (level & 0x3C0) >> 6, [(level & 0x3F) << 2])
No we can create simple program that will set DAC output level to value which you select with up/down cursors and will read ADC input:
import time
import sys
import curses
import KAmodRPiADCDAC
screen = curses.initscr()
curses.noecho()
curses.cbreak()
screen.keypad(True)
try:
KAmod = KAmodRPiADCDAC.KAmodRPiADCDAC()
# Set VINmax to the same value as JP2 jumper on KAmod board
KAmod.setVINmax(5)
daValue = 0
while True:
# Write daValue to DAC
KAmod.writeMCP4716(daValue)
# Read voltage from ADC
voltage = KAmod.readMCP3021Voltage()
screen.addstr('\rADC Voltage = %.2fV, DAC Value = %d ' % (voltage, daValue))