Nuvoton Project: Analog Input with Potentiometer

Introduction
Hi, this is David Tseng of CAVEDU Education. This is the third article of Nuvoton Numaker-PFM-M487. We’ve covered the introduction of this board and finished the hello world MBed OS example and the RGB-LED example in the previous articles. If you are not familiar with M487 yet, please refer to the previous articles:
In this topic, we are going to show you how to display user-defined / system messages and sensor data from your M487 board, the latter we will use an easy-to-get component, potentiometer. You can use it as an analog input to control your outputs more smoothly.
Hardware setup
This section will show you how to finish this example step by step with ARM mbed OS, let’s begin!
1. Get the parts ready
Prepare the parts below:
- NuMaker-PFM-M487 dev board, 1
- 500-ohm potentiometer, 1
- LED, 1
- PC (with Windows OS) with network connection to access mbed OS online IDE, 1
2. Driver setup
Refer to the M487 page or our previous articles to setup the driver on your system. If everything is correct, your m487 will be recognized by your OS as a disk named as NuMicro MCU.
3. Connect the potentiometer to your m487
We use the Arduino pin A0 on the m487 board. M487 provides Arduino UNO compatible interface, please refer to M487 page for detailed pin information.
To connect the components required to your M487, we use five male/male jumper wires and a half-size breadboard to finish the connection. Connect the middle pin of the potentiometer to the A0 pin of your M487 board (green wire). The other two pins are connected to the 5V / GND pins respectively. You can see that the M487 5V pin is connected to the potentiometer left pin by the red wire; and the M487 GND pin is connected to the potentiometer right pin by the black wire. These two pins only effect the increase/decrease while you rotate the knob, as shown in below:
For quick connection, you can directly connect the LED into the D13 pin to complete the EX01, the pin next to it is perfectly the GND pin. In EX02, we will control the LED fading effect by rotating the potentiometer, therefore the LED must connect to the pins supporting PWM, for example, D6 pin.
Of course, you can directly use D6 pin in EX01, feel free to do that!
EX01 – use the potentiometer to control LED on/off and read in PuTTy
In EX01, we will read the analog pin status which connected a potentiometer. The potentiometer is a resistor which resistance will vary by rotating the knob. According to the ohm’s law, you can tell that the voltage is proportional to the resistance:
V (voltage) = I (current) * R (resistance)
If you had finished the previous LED example, your mbed console should already have the M487. If not, please refer to the previous article to finish the setup process.
Please follow the steps below to finish today’s examples on the mbed website.
Step1:
Visit the MBED OS website, login with your account.
Step2:
After logged in, click the upper-right [Compiler] icon to enter the online compiler interface. Your NuMaker-PFM-M487 board and all projects should be there already, click the board to check all the details.
Step3:
In the M487 page, you can find the detailed information of this board and many starter examples from the right-hand side column.
Step4:
If you have not added M487 into your Mbed Compiler, click the [Add to you Mbed Compiler] to add this board into your compiler.
Step5:
Back to your compiler page, you can see the [NuMaker-PFM-M487] icon. Click [New] and select [Numaker ADC read gas sensor (MQ3)] in the Template dropdown menu. This will import the example into your compiler directly. Although the MQ3 gas sensor is mentioned, but this example is used to demonstrate how to read the analog pin status, so we use a potentiometer to replace the MQ3 gas sensor. You can get one BQ3 in the future if you like.
Click to open main.cpp.
Numaker ADC read gas sensor (MQ3) / main.cpp
// NuMaker-PFM-NUC472 use ADC0 (A0/PE_1) to read gas sensor (MQ-3) #include "mbed.h" /* NOTE: Connect UNO A0 to MQ-3 gas sensor for this test */ AnalogIn adc0(A0); DigitalOut led(D13); int main(void) { #ifdef MBED_MAJOR_VERSION printf("Mbed OS version %d.%d.%d\r\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); #endif printf("ADC0 start reading...\n\r"); while (1) { printf("ADC0 value= 0x%4x, percentage= %3.3f%%\n\r",adc0.read_u16(), adc0.read()*100.0f); if(adc0 0.7f) led = 1; // > 0.7 * Vcc wil turn on led else led = 0; wait(0.2f); #if MBED_MAJOR_VERSION >= 6 ThisThread::sleep_for(200); #else> wait(0.2f); #endif } }
In the while loop (#15), it will repeatedly print the A0 pin status read by the adc0.read_u16() and adc0.read() method, which is a decimal number between 0 and 1. If adc0 is greater than 0.7, the D13 LED(#17) will be turned on, or will be turned off when adc0 is less than 0.7 (#18).
According to the mbed documentation(https://os.mbed.com/handbook/AnalogIn), the adc0.read_u16() is the 16-bit normalized values, and the adc0.read() is the decimal number representing the voltage.
Step6:
Click the [Compile] icon to compile this project. After that, the compiled file will be downloaded to your PC as .bin extension. For this example, it should be “NuMaker-mbed-ADC0_MQ3.NUMAKER_PFM_M487.bin” or another file name you’d like to have.
Step7 – play!
Drag the .bin file into the “NuMicro-MCU” disk drive (yeah, it should appear when you connect the dev board to your PC). Rotate the knob of the potentiometer gently, and the LED will light on when the knob is over a certain point, as shown below:
Read output in serial communication software
Wait, what if we want to check the sensor data? Unlike Arduino IDE has a built-in serial monitor, here we have to use serial communication software to read output messages from the dev board. You can choose any software you like, e.g. PuTTy, CoolTerm, etc. Here I’ll use PuTTy.
In order to read m487’s output message, it has to set the switches correctly. On the same side of the micro-USB port, there is a four-in-one switches. Please toggle them when you are connecting PuTTY and are uploading .bin file. Alright, I know it’s somehow annoying because these switches are really small so you must get a tool to toggle them and during the development phase, you have to toggle them repeatedly… well, practice makes perfect!
for connecting PuTTy | for uploading .bin |
---|---|
1 ON 2 ON 3 ON 4 OFF | 1 OFF 2 OFF 3 OFF 4 ON |
![]() | ![]() |
Download PuTTY according to the platform you are using, the process should be easy. Open the PuTTy and click the Serial option on the left-hand side, and finish all the settings, as below. Please notice that you should open the device manger (for Windows OS), the COM4 is what my OS recognize the M487 board connect and it may NOT be your case.
After the settings are completed, click [Open], there will be a new window with all the messages (as the result of the #16 code), as below:
EX02 – use the potentiometer to control LED fading
Now connect the LED from D13 to D6, we are going to control the LED by rotating the potentiometer. This is a very common example of analog input / output demo.
Clone the EX01 project to a new project named as Numker-analog-LED and modify the main.cpp as below:
Numaker-analog-LED / main.cpp
#include "mbed.h" AnalogIn adc0(A0); PwmOut led(D6); float x; int main() { while (1) { x = adc0.read(); led.write(x); printf("%3.3f%\n\r", x); wait(0.1f); } }
The logic is just the same as EX01, except we removed the if/else and use the adc0.read() as the led.write() parameter to control the LED fading effect. Compile and drag the .bin into your NuMicro MCU drive. After flashing the code, rotate the knob and you should see the LED getting brighter/darker gradually., as shown in below video:
Challenge in 5 minutes
1. Modify EX01, use another LED (connect to D11 for example). Use the same if/else to control these two LEDs while one is on and the other is off.
2. Modify EX02, to adjust the fading effect in the opposite direction (hint: you can modify the code or the circuit).
More examples you can try
● AudioPlayback
● play note
● TCP connection
More info
● https://www.nuvoton.com/products/microcontrollers/arm-cortex-m4-mcus/m487-ethernet-series/?tab=4
● Nuvoton NuMaker-IoT-M487 series