Lesson 10 - Voltage meter





Following on from the previous lesson, we will be programming the Arduino against a specific specification to build a system which can have a real life application. This will allow us to apply what we have learnt throughout the course.


Program Description

The program we will be coding in this lesson is a Voltage Meter. We will be able to adjust the voltage reading between 0 and 5 volts. However, this system will include upper and lower trip points, which are defined in the specification. This trip points are used to ensure that the voltage level is not below the required level and also does not exceed the required level.


Program Specification

1. Using the variable resistor on the Multi Functional Shield, read the analog voltage.

2. The voltage reading must have an upper and lower trip point. The upper trip point is set at 3 volts and lower trip point is set at 2 volts.

3. When the meter reads the voltage within the trip point boundaries, the reading must be displayed to the 4x7 segment display.

4. But when the upper trip point is activated, a relevant message is to be displayed as wells as a visual LED indication.

5. Similarly, when the lower trip point is activated, a relevant message is to be displayed and an alternative LED visual indication.





Coding

Note: We will be going through the steps in completing this program as all programming techniques and functions required for this program have already been covered in previous lesson through the course. The final source code will be included at the end. Remember on the MFS the digital commands are reversed, HIGH = LOW and LOW = HIGH. Refer to the source code for guidance.


Global Scope - To begin with, in the global scope you want to include all the required libraries for this program. We will only require libraries to use the 4x7 segment display, so therefore we will include the MFS library and the timer one library. Also in the global scope, we will declare our global variables which can be accessed by all the functions in the program. These will include the two LED pins for the upper and lower trip points. Assign relevant variable names to each pin.


Setup - In the program setup, we will configure the pin modes as outputs for both our LEDs. And then we will initialise the timer library first, followed by the MFS library which is initialised in conjunction with the timer.


Loop - The first step we will take in writing our main program is to be able to calculate a voltage reading from the potentiometer. The resistance value range between 0 - 1023. We will create a new variable, float, and assign a variable name to it because this variable will be our voltage reading. To change the values from 0-1023 to a range that corresponds to the voltage the pin is reading, we need to scale the the numbers from 0.0 and 5.0. Simply, divide 5 by 1023 and then multiply it by the the potentiometer analog read:




float voltage = analogRead(A0) * (5.0 / 1023.0);





Once we have a variable which can calculate the voltage reading, we then move on and write our instructions to detect the trip points. This can be easily accomplished by using conditional statements. We can identify that if the voltage exceeds 3 volts, we can turn on an LED and write a high voltage message to the MFS display. Otherwise, if the voltage drops below 2 volts, we can turn on the other LED and write a low voltage message to the MFS display. And if neither of the first conditions are true, we can turn both LEDs off and just write the voltage read directly to the MFS.


Below you can find the completed source code for this project and can trial it on your own board. This system has real life applications and can be developed to include additional features and functions.




Course Completion Certificate


The team at InnoVech would like to thank you for joining us on this Arduino Course. We hope to have covered key areas of Arduino Programming and taught techniques which can be used to program complex systems.


We would like to provide you with recognition for completing this course and you can add this to your LinkedIn profile using the button below.


Name : Complete Guide to Arduino

Issuing Organisation: InnoVech (Consumer Electronics)

Check the box: 'This credential does not expire'






LinkedIn Add to Profile button


Source Code



We recommend copying the source code to the IDE



Code


By Zaqyas Mahmood, Electronics Engineer