Analog IO
I was wondering what the conversion speed and other specifications for the LinkIt were?
I came across this webpage (for the Intel Edison Galileo):
https://navinbhaskar.wordpress.com/2016/04/25/cc-on-intel-edisongalileo-part4adc/
It had an example of reading a potentiometer (i.e. a variable resistor) that controlled (i.e. PWM) the brightness of an LED.
The page reinforced the idea of properly shutting down the program. Okay, I get the message.
I was a bit surprised it compiled because of the problem with mraa_hello of "common.h" not being referenced in "mraa.h" but then I remembered that if you call any other set of functions they contain "common.h".
I don't understand the "(arg);" in the signal handler, it seems like some "compiler trick":
// Signal handler used to stop this application cleanly
void handler(int arg) {
(arg);
keepRunning = 0;
}
But otherwise the code is straight forward.
I was also a little surprised that "usleep(500);" did not ask for the "unistd.h" header.
But after I checked the "mraa" function headers I found:
- #include <stdio.h>
- #include <unistd.h>
- #include <stdint.h>
So problem solved, and now I understand the difference between <*.h> and "*.h", the first version reads any additional include files in the include file while the second version does not.
Here is my slightly edited version:
#include <stdio.h>
#include <stdint.h>
#include <mraa.h>
#include <signal.h>
#include <unistd.h>
#define PWM_PIN 5 // The pin where the LED is connected
#define ADC_PIN 0 // The ADC pin
#define ADC_MAX (1024.0) // Maximum value output by a 10-bit ADC
volatile int keepRunning;
// Signal handler used to stop this application cleanly
void handler(int arg) {
(arg);
keepRunning = 0;
}
int main(void) {
mraa_pwm_context pwmPin; // The PWM pin context
mraa_aio_context adcPin; // ADC pin context
float intensity; // Intensity with which LED is to glow
float adcValue; // Read ADC value
// Step 1: Intialize the mraa system
mraa_init();
// Step 2: Initalize analog port 0 for ADC operation
adcPin = mraa_aio_init(ADC_PIN);
// Step 3: Initialize D5 for PWM operation
pwmPin = mraa_pwm_init(PWM_PIN);
// Step 4: Set the period on the PWM pin
mraa_pwm_period_us(pwmPin, 5000); // Set the period as 5000 us or 5ms
/* Step 5: Enable the PWM pulse on the pin
mraa_pwm_enable(pwmPin, 1);
keepRunning = 1;
/*
* Associate ctrl+c with our handler that clears the 'keepRunning'
* flag that allows us to stop the PWM and ADC when exiting
*/
signal(SIGINT, handler);
while (keepRunning) {
// Step 6: Read the ADC value using the function 'mraa_aio_read()'
adcValue = mraa_aio_read(adcPin);
intensity = adcValue/ADC_MAX;
// Step7: Set the intensity of the LED at port D5 using PWM
mraa_pwm_write(pwmPin, intensity);
usleep(500);
}
// Step 8: Stop the PWM and ADC when not required
mraa_pwm_enable(pwmPin, 0);
mraa_aio_close(adcPin);
return 0;
}
Now if I read it correctly:
- "PWM_PIN 5" is GPIO5 and maps to Pin20 on the LinkIt.
- "ADC_PIN 0" is GPIO0 and maps to Pin12 on the LinkIt.
I am not sure what the ARef (the analog reference voltage) is, I am going to guess it is 3.3v rather than 5v, as the CPU is running at 3.3v.
I still don't know what the conversion speed is? I could measure it as it will likely be a blocking function.
Okay, now I know why I can not find anything on ADC for the LinkIt Smart MT7688, it does not have ADC! The LinkIt Smart MT7688 Duo has ADC however but still the key specifications are missing.
So if I want ADC I will have to use an external card.
AlanX
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.