Skip to content

Commit a1e90d2

Browse files
committed
Add arduino examples
1 parent acf1246 commit a1e90d2

File tree

65 files changed

+5683
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+5683
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
// This is just an empty header file to make this a valid Arduino library
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Title: ReadAnalogInput
3+
*
4+
* Objective:
5+
* This example demonstrates how to read the analog voltage of an analog
6+
* input. ClearCore analog inputs are natively compatible with 0-10V signals,
7+
* and 0-20mA signals with addition of an external resistor (see ClearCore
8+
* manual link below for wiring).
9+
*
10+
* Description:
11+
* This example sets up pin A-12 as an analog input, queries the value on
12+
* that connector every second, and calculates the input voltage. This
13+
* calculated voltage is written/displayed to the USB serial port.
14+
* Connectors IO-0 through IO-5 will act as a coarse meter of the voltage
15+
* read-in by turning on their outputs and LEDs (with more than 0V and less
16+
* than 2V, IO-0 will be on as an output. With ~2V and less than 4V, IO-0 and
17+
* IO-1 will be on; and so on until, with near 10V read in, IO-0 through IO-5
18+
* will be turned on).
19+
*
20+
* Requirements:
21+
* ** An analog input source connected to A-12.
22+
* ** Optional: LEDs connected to IO-0 through IO-5 to act as a more prominent
23+
* voltage meter than the ClearCore's onboard LEDs.
24+
*
25+
* Links:
26+
* ** web link to doxygen (all Examples)
27+
* ** web link to ClearCore Manual (all Examples) <<FUTURE links to Getting started webpage/ ClearCore videos>>
28+
*
29+
* Last Modified: 1/21/2020
30+
* Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
31+
* the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
32+
*/
33+
34+
// Defines the bit-depth of the ADC readings (8-bit, 10-bit, or 12-bit)
35+
// Supported adcResolution values are 8, 10, and 12
36+
#define adcResolution 12
37+
38+
// Select the baud rate to match the target device.
39+
#define baudRate 9600
40+
41+
void setup() {
42+
// Put your setup code here, it will only run once:
43+
44+
// Initialize the serial port for printing analog voltage readings and wait
45+
// up to 5 seconds for a port to open. Serial communication is not required
46+
// for this example to run, however without it only the coarse LED meter
47+
// can be used to read the analog signal.
48+
Serial.begin(baudRate);
49+
uint32_t timeout = 5000;
50+
uint32_t startTime = millis();
51+
while (!Serial && millis() - startTime < timeout) {
52+
continue;
53+
}
54+
55+
// Make a voltage meter display with the I/O pins.
56+
pinMode(IO0, OUTPUT);
57+
pinMode(IO1, OUTPUT);
58+
pinMode(IO2, OUTPUT);
59+
pinMode(IO3, OUTPUT);
60+
pinMode(IO4, OUTPUT);
61+
pinMode(IO5, OUTPUT);
62+
63+
// Clear out the state of our voltage meter to start.
64+
digitalWrite(IO0, false);
65+
digitalWrite(IO1, false);
66+
digitalWrite(IO2, false);
67+
digitalWrite(IO3, false);
68+
digitalWrite(IO4, false);
69+
digitalWrite(IO5, false);
70+
71+
// Since analog inputs default to analog input mode, there's no need to
72+
// call pinMode().
73+
74+
// Set the resolution of the ADC.
75+
analogReadResolution(adcResolution);
76+
}
77+
78+
void loop() {
79+
// Put your main code here, it will run repeatedly:
80+
81+
// Read the analog input (A-9 through A-12 may be configured as analog
82+
// inputs).
83+
int adcResult = analogRead(A12);
84+
// Convert the reading to a voltage.
85+
double inputVoltage = 10.0 * adcResult / ((1 << adcResolution) - 1);
86+
87+
// Alternatively, you can use the following to get a measurement in
88+
// volts:
89+
// inputVoltage = analogRead(A12, MILLIVOLTS) / 1000.0;
90+
91+
// Display the voltage reading to the USB serial port.
92+
Serial.print("A-12 input voltage: ");
93+
Serial.print(inputVoltage);
94+
Serial.println("V.");
95+
96+
// Write the voltage reading to the voltage meter display pins
97+
// (IO-0 through IO-5).
98+
if (inputVoltage > 0.1) {
99+
digitalWrite(IO0, true);
100+
}
101+
else {
102+
digitalWrite(IO0, false);
103+
}
104+
if (inputVoltage > 2.0) {
105+
digitalWrite(IO1, true);
106+
}
107+
else {
108+
digitalWrite(IO1, false);
109+
}
110+
if (inputVoltage > 4.0) {
111+
digitalWrite(IO2, true);
112+
}
113+
else {
114+
digitalWrite(IO2, false);
115+
}
116+
if (inputVoltage > 6.0) {
117+
digitalWrite(IO3, true);
118+
}
119+
else {
120+
digitalWrite(IO3, false);
121+
}
122+
if (inputVoltage > 8.0) {
123+
digitalWrite(IO4, true);
124+
}
125+
else {
126+
digitalWrite(IO4, false);
127+
}
128+
if (inputVoltage >= 9.9) {
129+
digitalWrite(IO5, true);
130+
}
131+
else {
132+
digitalWrite(IO5, false);
133+
}
134+
135+
// Wait a second before the next reading.
136+
delay(1000);
137+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Title: WriteAnalogCurrentOutput
3+
*
4+
* Objective:
5+
* This example demonstrates how to write analog current values to an analog
6+
* current output connector.
7+
*
8+
* Description:
9+
* This example configures pin IO-0 as an analog current output. It outputs
10+
* a repeating analog signal, starting at 0mA, increasing to 20mA, and
11+
* decreasing back to 0mA.
12+
*
13+
* Requirements:
14+
* ** Connect a device to IO-0 which takes in analog current.
15+
*
16+
* Links:
17+
* ** web link to doxygen (all Examples)
18+
* ** web link to ClearCore Manual (all Examples) <<FUTURE links to Getting started webpage/ ClearCore videos>>
19+
*
20+
* Last Modified: 1/21/2020
21+
* Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
22+
* the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
23+
*/
24+
25+
26+
void setup() {
27+
// Put your setup code here, it will run once:
28+
29+
// Set up connector IO-0 to be an output. We will specify that it outputs
30+
// current in our analogWrite functions later (as opposed to a digital
31+
// signal or PWM).
32+
// Note that only connector IO-0 is capable of analog current output.
33+
pinMode(IO0, OUTPUT);
34+
}
35+
36+
void loop() {
37+
// Put your main code here, it will run repeatedly:
38+
39+
// Ramp the current output of IO-0 up to 20mA.
40+
for (uint16_t value = 0; value < 2047; value++) {
41+
// ClearCore's analog current output has 11-bit resolution, so we write
42+
// values of 0 to 2047 (corresponding to 0-20mA).
43+
analogWrite(IO0, value, CURRENT);
44+
delay(2);
45+
}
46+
47+
// Ramp the current output of IO-0 back down.
48+
for (uint16_t value = 0; value < 2047; value++) {
49+
analogWrite(IO0, 2047 - value, CURRENT);
50+
delay(2);
51+
}
52+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Title: WritePwmOutput
3+
*
4+
* Objective:
5+
* This example demonstrates how to write a digital PWM signal to a ClearCore
6+
* digital output.
7+
*
8+
* Description:
9+
* This example sets the defined pin as an output then writes a series of
10+
* PWM signals with varying duty cycles to the output.
11+
*
12+
* Requirements:
13+
* ** Connect a device that takes in a PWM signal to IO-1
14+
*
15+
* Links:
16+
* ** web link to doxygen (all Examples)
17+
* ** web link to ClearCore Manual (all Examples) <<FUTURE links to Getting started webpage/ ClearCore videos>>
18+
*
19+
* Last Modified: 1/21/2020
20+
* Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
21+
* the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
22+
*/
23+
24+
// Specify which output pin to write digital PWM to.
25+
// PWM-capable pins: IO-0 through IO-5.
26+
// Note: IO-4 and IO-5 are capable of bi-directional and higher-current PWM
27+
// output using an H-Bridge. See the WriteHBridgeOutput example.
28+
#define outputPin IO1
29+
30+
void setup() {
31+
// Put your setup code here, it will run once:
32+
33+
// Set up the output pin to be an output
34+
pinMode(outputPin, OUTPUT);
35+
}
36+
37+
void loop() {
38+
// Put your main code here, it will run repeatedly:
39+
40+
// Write some digital PWM signals to the output connector.
41+
// Note: analogWrite() is the Arduino wrapper function used to write PWM,
42+
// despite the fact that it is not a "true" analog signal
43+
// Valid values range from 0 (0% duty cycle / always off)
44+
// to 255 (100% duty cycle / always on).
45+
46+
// Output a low duty cycle for 1 second.
47+
analogWrite(outputPin, 10);
48+
delay(1000);
49+
50+
// Output a medium duty cycle for 1 second.
51+
analogWrite(outputPin, 120);
52+
delay(1000);
53+
54+
// Output a high duty cycle for 1 second.
55+
analogWrite(outputPin, 230);
56+
delay(1000);
57+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Analog I/O
2+
version=1.0.0
3+
author=Teknic
4+
maintainer=Teknic <sales@teknic.com>
5+
sentence=Teknic Analog I/O Examples
6+
paragraph=Teknic Analog I/O Examples
7+
category=Device Control
8+
url=https://github.com/Teknic-Inc/ClearCore-Arduino-wrapper
9+
architectures=sam

0 commit comments

Comments
 (0)