1+ /*
2+ * Portenta Machine Control - Fast Analog In 0-10 V
3+ *
4+ * This example provides the voltage value acquired by the Machine Control using the Arduino_AdvancedAnalog library,
5+ * which is designed to offer enhanced performance in terms of acquisition speed compared to the MachineControl_AnalogIn feature.
6+ * For each channel of the ANALOG IN connector, there is a resistor divider made by a 100k and 39k; the input voltage is divided by a ratio of 0.28.
7+ * The maximum input voltage is 10V.
8+ * To use the 0V-10V functionality, a 24V supply on the PWR SUPPLY connector is necessary.
9+ *
10+ * The circuit:
11+ * - Portenta H7
12+ * - Portenta Machine Control
13+ *
14+ * Initial author: Leonardo Cavagnis @leonardocavagnis
15+ */
16+
17+ #include < Arduino_MachineControl.h>
18+ #include < Arduino_AdvancedAnalog.h>
19+
20+ const float RES_DIVIDER = 0.28057 ;
21+ const float REFERENCE = 3.0 ;
22+
23+ // A3 is connected to PMC-AI0, A2 is connected to PMC-AI1
24+ AdvancedADC MachineControl_FastAnalogIn01 (A3, A2); // A3 & A2 share the same ADC instance (ADC3)
25+ // A1 is connected to PMC-AI2
26+ AdvancedADC MachineControl_FastAnalogIn2 (A1);
27+
28+ uint16_t adc_get_buf (AdvancedADC &adc);
29+ void adc_get_buf (AdvancedADC &adc, uint16_t * sample_buf, uint8_t sample_buf_size);
30+
31+ void setup () {
32+ Serial.begin (9600 );
33+ while (!Serial) {
34+ ; // wait for serial port to connect.
35+ }
36+
37+ // Configure the sensor type 0-10V
38+ MachineControl_AnalogIn.begin (SensorType::V_0_10);
39+
40+ // Initialize the Advanced Analog feature on PMC AI pins
41+ if (!MachineControl_FastAnalogIn01.begin (AN_RESOLUTION_16, 8000 , 32 , 3 )) {
42+ Serial.println (" AI0, AI1: Failed to start analog acquisition!" );
43+ while (1 );
44+ }
45+
46+ if (!MachineControl_FastAnalogIn2.begin (AN_RESOLUTION_16, 8000 , 32 , 3 )) {
47+ Serial.println (" AI2: Failed to start analog acquisition!" );
48+ while (1 );
49+ }
50+ }
51+
52+ void loop () {
53+ uint16_t raw_voltage_ch01[2 ] = {0 , 0 };
54+ uint16_t raw_voltage_ch2 = 0 ;
55+
56+ adc_get_buf (MachineControl_FastAnalogIn01, raw_voltage_ch01, 2 );
57+ float voltage_ch0 = ((float )raw_voltage_ch01[0 ] * REFERENCE) / 65535 / RES_DIVIDER;
58+ Serial.print (" Voltage CH0: " );
59+ Serial.print (voltage_ch0, 3 );
60+ Serial.println (" V" );
61+
62+ float voltage_ch1 = ((float )raw_voltage_ch01[1 ] * REFERENCE) / 65535 / RES_DIVIDER;
63+ Serial.print (" Voltage CH1: " );
64+ Serial.print (voltage_ch1, 3 );
65+ Serial.println (" V" );
66+
67+ raw_voltage_ch2 = adc_get_buf (MachineControl_FastAnalogIn2);
68+ float voltage_ch2 = ((float )raw_voltage_ch2 * REFERENCE) / 65535 / RES_DIVIDER;
69+ Serial.print (" Voltage CH2: " );
70+ Serial.print (voltage_ch2, 3 );
71+ Serial.println (" V" );
72+
73+ Serial.println ();
74+ delay (250 );
75+ }
76+
77+ uint16_t adc_get_buf (AdvancedADC &adc) {
78+ uint16_t sample = 0 ;
79+
80+ if (adc.available ()) {
81+ SampleBuffer buf = adc.read ();
82+
83+ // Print first sample.
84+ sample = buf[0 ];
85+ Serial.println (sample);
86+
87+ // Release the buffer to return it to the pool.
88+ buf.release ();
89+ }
90+
91+ return sample;
92+ }
93+
94+ void adc_get_buf (AdvancedADC &adc, uint16_t * sample_buf, uint8_t sample_buf_size) {
95+ if (adc.available ()) {
96+ SampleBuffer buf = adc.read ();
97+
98+ for (uint8_t pos = 0 ; pos < sample_buf_size; pos++) {
99+ sample_buf[pos] = buf[pos];
100+ Serial.println (sample_buf[pos]);
101+ }
102+
103+ // Release the buffer to return it to the pool.
104+ buf.release ();
105+ }
106+
107+ return ;
108+ }
0 commit comments