Actions

Difference between revisions of "KAmodRPi ADC DAC"

Line 1: Line 1:
__jzpdf__
 
====== Opis ======
 
[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] to moduł z 10-bitowym przetwornikiem analogowo-cyfrowym MCP3021 oraz z 10-bitowym przetwornikiem cyfrowo-analogowym MCP4716. Oba układy wykorzystują magistralę I2C. Zakres napięcia wejściowego przetwornika ADC ustalany może być za pomocą zworki; dostępne są trzy przedziały napięcia: 0 - 3,3 V ; 0 - 5 V; 0 - 10 V. Złącze przelotowe pozwala na dołączenie modułu do dowolnej wersji Raspberry Pi. Przykładowy program pokazuje, jak można obsłużyć moduł na komputerze Raspberry Pi, przy pomocy języka Python.
 
  
===== Wymagania =====
+
====== 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.
 +
Sample program will show how to use the module with Raspberry Pi using Python.
  
Do uruchomienia przykładowego kodu potrzebne będą:
+
===== Prerequisites =====
* Raspberry Pi (dowolna wersja)
 
* Karta z systemem operacyjnym Raspbian
 
* Moduł KAmodRPi ADC_DAC
 
* Zworka lub przewód
 
  
===== Sposób podłączenia =====
+
To run sample program you need
 +
* Raspberry Pi (any version)
 +
* SD card with Raspbian OS
 +
* KAmodRPi ADC_DAC module
 +
* One jumper connector or wire
  
* Nałożyć moduł KAmodRPi ADC_DAC na Raspberry Pi
+
===== Hardware setup =====
* Połączyć piny VIN oraz VOU zworką lub przewodem
 
  
[[Plik:Rpi_kamod_adc_dac.jpg]]
+
* Connect KAmodRPi ADC_DAC module to Raspberry Pi
 +
* Connect pins VIN and VOU with jumper or wire
  
===== Konfiguracja =====
+
[[File:Rpi_kamod_adc_dac.jpg]]
  
Pierwszą czynnością będzie uruchomienie obsługi I2C w Raspbianie; w tym celu wykonujemy komendę:
+
===== Configuration =====
 +
 
 +
First of all we need to enable I2C support in Raspbian, run:
  
 
  sudo raspi-config
 
  sudo raspi-config
  
W oknie konfiguracji wybieramy kolejno //Advanced Options//, //I2C//, //Yes// (dwukrotnie), po czym uruchamiamy ponownie Raspberry Pi. Następnie, zainstalować musimy i2c-tools komendą:
+
Select //Advanced Options//, //I2C//, then select //Yes// twice, then reboot Raspberry Pi. Now install i2c-tools:
  
 
  sudo apt-get install i2c-tools
 
  sudo apt-get install i2c-tools
  
Sprawdzimy, czy Raspberry Pi może połączyć się z modułem poprzez I2C:
+
Let's check if Raspberry Pi can communicate with module:
  
 
  i2cdetect -y 1
 
  i2cdetect -y 1
  
Rezultat komendy powinien wyglądać następująco:
+
Output should look like this:
  
 
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
 
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
Line 52: Line 53:
 
  70: -- -- -- -- -- -- -- --
 
  70: -- -- -- -- -- -- -- --
  
Jak widać, komputer odnalazł dwa adresy urządzeń I2C - rozpoznajmy je:
+
We can see that Raspberry Pi found two I2C devices:
* 0x4d - adres układu MCP3021
+
* MCP3021 - address 0x4d
* 0x60 - adres układu MCP4716
+
* MCP4716 - address 0x60
  
===== Kod programu w Pythonie =====
+
===== Python code =====
Do obsługi magistrali I2C w Pythonie musimy zainstalować bibliotekę smbus:
+
To be able to use I2C in Python we need to install smbus library:
<code>sudo apt-get install python-smbus</code>.
+
<code>sudo apt-get install python-smbus</code>
Mając zainstalowaną tą bibliotekę, umieścimy poniższy kod w pliku KAmodRPiADCDAC.py:
+
Now you can use simple library, put the code below into file KAmodRPiADCDAC.py:
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
import smbus
 
import smbus
Line 85: Line 86:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
   
 
   
 
+
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:
Możemy teraz utworzyć prosty program, który ustawi poziom wyjścia na wyjściu przetwornika DAC na wartość, którą zmieniać będziemy kursorami (góra/dół).  Jednocześnie, program wyświetli wartość z wejścia przetwornika ADC:
 
  
  
Line 104: Line 105:
 
         KAmod = KAmodRPiADCDAC.KAmodRPiADCDAC()
 
         KAmod = KAmodRPiADCDAC.KAmodRPiADCDAC()
  
         # Ustawienie VINmax na tą samą wartość, co zworka JP2 na płytce KAmod
+
         # Set VINmax to the same value as JP2 jumper on KAmod board
 
         KAmod.setVINmax(5)
 
         KAmod.setVINmax(5)
  
Line 110: Line 111:
 
         while True:
 
         while True:
  
                 # Zapisz daValue do przetwornika DAC
+
                 # Write daValue to DAC
 
                 KAmod.writeMCP4716(daValue)
 
                 KAmod.writeMCP4716(daValue)
  
                 # Odczytaj voltage z przetwornika ADC
+
                 # Read voltage from ADC
 
                 voltage = KAmod.readMCP3021Voltage()
 
                 voltage = KAmod.readMCP3021Voltage()
  
Line 124: Line 125:
  
  
===== Do pobrania =====
+
===== Links =====
* [[Plik:Kamodrpiadcdac.zip]]
+
* [[File:Kamodrpiadcdac.zip]]
===== Linki zewnętrzne =====
+
[[Category:Extension modules for Raspberry Pi]]
* [http://mikrokontroler.pl/2016/10/25/projekt-kamodrpi-10b-adcdac-ekspander-przetwornikiem-adc-dla-komputerow-raspberry-pi-przyklad-c/ Artykuł na portalu MIKROKONTROLER.PL: KAmodRPI 10b ADC/DAC – ekspander z przetwornikiem ADC dla komputerów Raspberry Pi – Przykład w C++]
+
[[Category:Content]]
[[Kategoria:Moduły dla Raspberry Pi]]
 
[[Kategoria:Zawartość]]
 

Revision as of 16:36, 4 July 2018

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

Rpi kamod adc dac.jpg

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))



Links