Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Arduino MCP4728 #29

Merged
merged 11 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions src/Switch-Arduino_MCP4728/Switch-Arduino_MCP4728.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#include <Adafruit_MCP4728.h>

Adafruit_MCP4728 working_mcp;
Adafruit_MCP4728 mcp0;
Adafruit_MCP4728 mcp1;
Adafruit_MCP4728 mcp2;
Adafruit_MCP4728 mcp3;
Adafruit_MCP4728 mcp4;
Adafruit_MCP4728 mcp5;
Adafruit_MCP4728 mcp6;
Adafruit_MCP4728 mcp7;

Adafruit_MCP4728 mcp_list[8] = {
mcp0,
mcp1,
mcp2,
mcp3,
mcp4,
mcp5,
mcp6,
mcp7,
};

bool mcp_is_initialized[8] = {
false,
false,
false,
false,
false,
false,
false,
false,
};

uint8_t addresses[8] = {
0x60,
0x61,
0x62,
0x63,
0x64,
0x65,
0x66,
0x67,
};

String command;

channel channels[4] = {
MCP4728_CHANNEL_A,
MCP4728_CHANNEL_B,
MCP4728_CHANNEL_C,
MCP4728_CHANNEL_D,
};

int v_ref = MCP4728_VREF_VDD;
int v_gain = MCP4728_GAIN_1X;


void setup() {
// put your setup code here, to run once:
Serial.begin(115200);

// Initliazation finished
Serial.println("Ready");
}


void loop() {
// put your main code here, to run repeatedly:

if (Serial.available() > 0) {

command = Serial.readStringUntil('\n'); // make sure you have '\n', "\n" does not work
command.toUpperCase(); // commands are not case-sensitive
command.replace(" ", ""); // removes all white spaces

// Connect MCP with specific I2C Address (0-7)
if (command.startsWith("AD=")) {
int address = command.substring(command.indexOf('=')+1).toInt();
set_mcp(address);
}

// Set Value for Channel: e.g. CH4=1000
else if (command.startsWith("CH")) {
int channel = command.substring(command.indexOf('H')+1, command.indexOf('=')).toInt();
int voltage = command.substring(command.indexOf('=')+1).toInt();

set_voltage(channel, voltage);
}

// Set Reference Voltage: VR=E or VR=I (external or internal)
else if (command.startsWith("VR")) {
String source = command.substring(command.indexOf('=')+1);
set_vref(source);
}

// Set Gain for internal reference voltage: GN=1 or GN=2 (1X or 2X)
else if (command.startsWith("GN")) {
int gain = command.substring(command.indexOf('=')+1).toInt();
set_gain(gain);
}
}
}

// ------------- Initialize MCP ------------------------
void set_mcp(int address) {
// If the MCP is not started yet, initialize new
if (!mcp_is_initialized[address]) {
if (!mcp_list[address].begin(addresses[address])) {
// Initialization Failed
Serial.print("NAK");
Serial.println(address);
}
else {
// Initialization Succesfull
mcp_is_initialized[address] = true;

// Assign working_mcp to it
working_mcp = mcp_list[address];

Serial.print("ACK");
Serial.println(address);
}
}
else {
// If the MCP is already initialized, set working_mcp
working_mcp = mcp_list[address];
Serial.print("ACK");
Serial.println(address);
}
}

// ------------- Set Voltage ------------------------
void set_voltage(int channel, int voltage) {
if (channel > 3 || voltage > 4095 || voltage < 0) {
// Maximum 4 channels, Prevent overflow of voltage.
Serial.println("NAK");
}
else {
working_mcp.setChannelValue(channels[channel], voltage, v_ref, v_gain);
Serial.print("ACK");
Serial.print(channel);
Serial.print("=");
Serial.println(voltage);
}
}

// ------------- Set Reference ------------------------
void set_vref(String source) {
if (source == "E") {
v_ref = MCP4728_VREF_VDD;
Serial.println("ACKE");
}
else if (source == "I") {
v_ref = MCP4728_VREF_INTERNAL;
Serial.println("ACKI");
}
else {
Serial.println("NAK");
}
}

// ------------- Set Gain ------------------------
void set_gain(int gain) {
if (gain == 1) {
v_gain = MCP4728_GAIN_1X;
Serial.println("ACK1");
}
else if (gain == 2) {
v_gain = MCP4728_GAIN_2X;
Serial.println("ACK2");
}
else {
Serial.println("NAK");
}
}
37 changes: 37 additions & 0 deletions src/Switch-Arduino_MCP4728/example/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import time

import pysweepme

driver_name = "Switch-Arduino_MCP4728"
driver_path = r"path_to\instrument-drivers\src" # Path to the instrument-driver repository
port_string = "COM1" # You can get the COM Port from Arduino IDE or SweepMe

# Initialize Driver
mcp = pysweepme.get_driver(driver_name, driver_path, port_string)

# Set measurement parameter
mcp.set_parameters(
{
"Channel": "all",
"I2C Address": "0,1,2,3",
"SweepMode": "Voltage in V",
"Voltage reference": "External",
"External voltage in V": 5.0,
},
)

# Initialize Device
mcp.connect()
mcp.configure()

# Set output values as colon-divided string
values = "0.15:0.25:0.35:0.45:1.19:1.29:1.39:1.49:2.19:2.29:2.39:2.49:3.19:3.29:3.39:3.49"
mcp.set_value(values)
mcp.apply()

# optional wait
time.sleep(5)

# Disconnect and unconfigure at the end to set voltages back to 0
mcp.unconfigure()
mcp.disconnect()
26 changes: 26 additions & 0 deletions src/Switch-Arduino_MCP4728/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
This Device Class is published under the terms of the MIT License.
Required Third Party Libraries, which are included in the Device Class
package for convenience purposes, may have a different license. You can
find those in the corresponding folders or contact the maintainer.

MIT License

Copyright (c) 2024 SweepMe! GmbH (sweep-me.net)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading
Loading