-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmcp4725-test.py
59 lines (48 loc) · 1.77 KB
/
mcp4725-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'''
Python Script for seeting voltages to MCP4725 DAC unit
Use : Helps to check if DAC unit is working properly
'''
# Simple demo of setting the DAC value up and down through its entire range
# of values.
# Author: Tony DiCola, Codenio
import time
try:
import RPi.GPIO as GPIO
import board
import busio
import adafruit_mcp4725
except:
import VPi.GPIO as GPIO
import VPi.board as board
import VPi.busio as busio
import VPi.adafruit_mcp4725 as adafruit_mcp4725
# Initialize I2C bus.
i2c = busio.I2C(board.SCL, board.SDA)
# Initialize MCP4725.
dac = adafruit_mcp4725.MCP4725(i2c)
# Optionally you can specify a different addres if you override the A0 pin.
# amp = adafruit_max9744.MAX9744(i2c, address=0x63)
# There are a three ways to set the DAC output, you can use any of these:
dac.value = 65535 # Use the value property with a 16-bit number just like
# the AnalogOut class. Note the MCP4725 is only a 12-bit
# DAC so quantization errors will occur. The range of
# values is 0 (minimum/ground) to 65535 (maximum/Vout).
dac.raw_value = 4095 # Use the raw_value property to directly read and write
# the 12-bit DAC value. The range of values is
# 0 (minimum/ground) to 4095 (maximum/Vout).
dac.normalized_value = 1.0 # Use the normalized_value property to set the
# output with a floating point value in the range
# 0 to 1.0 where 0 is minimum/ground and 1.0 is
# maximum/Vout.
# Main loop will go up and down through the range of DAC values forever.
while True:
# Go up the 12-bit raw range.
print("Going up 0-3.3V...")
for i in range(4095):
dac.raw_value = i
time.sleep(5)
# Go back down the 12-bit raw range.
print("Going down 3.3-0V...")
for i in range(4095, -1, -1):
dac.raw_value = i
time.sleep(5)