2108 views|0 replies

1140

Posts

0

Resources
The OP
 

How to measure analog voltage using the ADC of MSP430? [Copy link]

One feature that is used in almost all embedded applications is the ADC module (Analog to Digital Converter). These ADCs can read the voltage from analog sensors like temperature sensors, tilt sensors, current sensors, flex sensors, etc. So, in this tutorial, we will learn how to read analog voltage using the ADC in MSP430G2 using the Energia IDE development environment. We will connect a small potentiometer to the MSP development board and apply a varying voltage on the analog pin and then read the voltage and display it on the serial monitor.

ADC Module Introduction

The process of connecting and programming the MSP430G2 to read analog voltages hardly takes 10 minutes. However, let’s first take a moment to understand the ADC module on the MSP development board so that we can use it effectively in other articles.

A microcontroller is a digital device, which means it can only recognize 1 and 0. But in the real world, temperature, humidity, wind speed, etc. are almost all analog quantities. In order to interact with these analog changes, the microcontroller uses a module called ADC. There are many different types of ADC modules to choose from, and the module used in our MSP development board is a SAR 8-channel 10-bit ADC.

Successive Approximation Register (SAR) ADC: SAR ADC has a comparator and some logic. This type of ADC uses a reference voltage (variable) and uses a comparator to compare the input voltage with the reference voltage and saves the difference from the most significant bit (MSB) as the digital output. The speed of the comparison depends on the clock frequency (Fosc) at which the MSP runs.

10-bit resolution: This ADC is an 8-channel 10-bit ADC. Here 8 channels means there are 8 ADC pins using which we can measure analog voltage. 10 bits means the resolution of the ADC. 10 bits means 2 to the power of 10, which is 1024. This is the number of sampling steps of our ADC, so our ADC value will range from 0 to 1023. Based on the value of each step voltage, the value will increase from 0 to 1023, which can be calculated using the following formula

In our previous tutorial, we have learned how to interface LCD with MSP430G2, now we just need to add a potentiometer to MSP430, provide it with a variable voltage and then display the voltage value on LCD. If you don’t know how to interface LCD, click on the link above to read it. The complete circuit diagram of this project is given below.

As shown in the figure, two potentiometers are used in this project, one for setting the contrast of LCD and the other for providing a variable voltage to the board. In this potentiometer, one end of the potentiometer is connected to Vcc and the other end is connected to ground. The center pin (blue wire) is connected to pin P1.7. This pin P1.7 will provide a variable voltage from 0V (ground) to 3.5V (Vcc). So we have to program pin P1.7 to read this variable voltage and display it on the LCD.

In Energia, we need to know which analog channel pin P1.7 belongs to? This can be found by referring to the picture below

You can see the P1.7 pin on the right side, which belongs to A7 (channel 7). Similarly, we can find the corresponding channel numbers of other pins as well. You can use any pin from A0 to A7 to read the analog voltage, here I have chosen A7.

Programming the MSP430

Programming the MSP430 to read analog voltage is very simple. In this program, we will read the analog value of the digital value and use it to calculate the voltage and then display it on the LCD screen. The complete program is listed at the end of this article, below I will explain the program in detail to help you understand it better.

We start by defining the LCD pins. These define which pin of the MSP430 the LCD pins are connected to. You can refer to your connections to make sure the pins are connected respectively

#define RS 2

#define EN 3

#define D4 4

#define D5 5

#define D6 6

#define D7 7

Copy code

Next, we include the header file for the LCD display. This will call the library that contains the code for how the MSP communicates with the LCD. This library is installed by default in the Energia IDE, so you don't need to add it. Also make sure that the Liquid Crystal functions are called with the pin names defined above.

#include <LiquidCrystal.h> //This librarey is insatlled by default along with IDE

LiquidCrystal lcd(RS, EN, D4, D5, D6, D7); //Let the librarey know how we have connected the LCD

Copy code

In the setup() function, we simply display an introductory message on the LCD screen.

lcd.begin(16, 2); //We are using a 16*2 LCD display

lcd.setCursor (0,0); //Place the cursor at 1st row 1st column

lcd.print("MSP430G2553"); //Display a intro message

lcd.setCursor(0, 1); // set the cursor to 1st column 2nd row

lcd.print("-CircuitDigest"); //Display a intro message

Copy code

Finally, in the loop() function, we start reading the voltage of the A7 pin. As we said above, the microcontroller is a digital device and it cannot read voltage levels directly. Using the SAR technique, the voltage levels are mapped from 0 to 1024. These values are called ADC values, and in order to get this ADC value, just use the following code:

int val = analogRead(A7); // read the ADC value from pin A7

Copy code

Here we use the analogRead() function to read the analog value of the pin, and since we have connected the variable voltage to pin P1.7, we have specified A7 in it. Finally, we save this value in a variable called "val". Since we can only get values from 0 to 1024 to store in this variable, we define the type of this variable to be integer.

The next step will be to calculate the voltage value from the ADC value. To do this, we use the following formula:

Voltage = (ADC Value / ADC Resolution) * Reference Voltage

Copy code

In this article, we already know that the ADC resolution of the microcontroller is 1024. The ADC value is also found in the previous line of code and stored in the variable called val. The reference voltage is equal to the voltage at which the microcontroller operates. When the MSP430 development board is powered through the USB cable, the operating voltage is 3.6V. You can also measure the operating voltage by using a multimeter on the Vcc and ground pins on the board. So the above formula is suitable for our case as shown below

float voltage = (float(val)/1024) * 3.6; //formulae to convert the ADC value to voltage

Copy code

You might not understand float(val). This is used to convert the variable "val" from an int data type to a "float" data type. This conversion is necessary because we can multiply by 3.6 only if we get the result of val / 1024 in floating point. If the value is received as an integer, it will always be 0 and the result will also be zero. Once we have calculated the ADC value and the voltage, all that remains is to display the result on the LCD screen, which can be achieved by using the following lines of code:

lcd.setCursor(0, 0); // set the cursor to column 0, line 0

lcd.print("ADC Val:");

lcd.print(val); //Display ADC value

lcd.setCursor(0, 1); // set the cursor to column 0, line 1

lcd.print("Voltage:");

lcd.print(voltage); //Display voltage

Copy code

Here we have displayed the ADC value in the first line and the voltage value in the second line. Finally we delay for 100 milliseconds and then erase the LCD display. The displayed value is refreshed every 100 milliseconds.

Test Results

Finally, we get back to the fun part, testing the program. Make the connections as shown in the circuit diagram. I used a small breadboard to make the connections and used jumper wires to connect the breadboard to the MSP430. Once the connections are made, it will look like this.

Then upload the program to the MSP430 development board through Energia IDE. You should be able to see an introduction message on the LCD, if not, use the potentiometer to adjust the contrast of the LCD until you see clear text. Alternatively, try pressing the reset button. If things work as expected, then you should see the following interface.

Now change the potentiometer and you should also see the voltage displayed on the LCD change. Let's verify that the voltage is measured correctly, use a multimeter to measure the voltage between the center of the POT and ground. The voltage displayed on the multimeter should be close to the value displayed on the LCD as shown in the following figure.

That's all for this article, we have introduced how to use the ADC of the MSP430 development board to measure analog voltage. Now we can connect many analog sensors with the development board and read the real-time parameters.

This post is from Microcontroller MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list