Tutorial 5
|
Material needed:Links on each, for where to buy it.
![]()
|
Explanation:
![Imagen](/uploads/3/2/0/3/32036615/6349219.gif?250)
What is the difference between an ANALOG signal and a DIGITAL signal? We have learned that our launchpad thinks digitally, this means that recognizes digital signals, this means HIGH or LOW, ON or OFF,1 or 0, if you think about it a DIGITAL signal is when there are just two choices YES or NO. In this tutorial we will open a new world of possibilities for our Launchpad with the help of ANALOG signals.
An analog signal can read HIGH and LOW but most importantly can also read EVERYTHING IN BETWEEN. While all the pins of our Launchpad can read a DIGITAL signal, only some pins can read an ANALOG signal. It is through these specialized pins analog input pins where we can get a better view of the outside world, and we can find out this special pins by going to the PINOUT (msp430 diagram) and look at the nicknames, the ones with an Ax on their nickname, those are analog pins. Ex,pin 2 is also A0 or pin 3 is also A1, this are some of the analog pins of our Launchpad. Another thing about the analog pins is that, you do not necessarily need to set it up as an input in the void setup(), you can but its optional.
An analog signal can read HIGH and LOW but most importantly can also read EVERYTHING IN BETWEEN. While all the pins of our Launchpad can read a DIGITAL signal, only some pins can read an ANALOG signal. It is through these specialized pins analog input pins where we can get a better view of the outside world, and we can find out this special pins by going to the PINOUT (msp430 diagram) and look at the nicknames, the ones with an Ax on their nickname, those are analog pins. Ex,pin 2 is also A0 or pin 3 is also A1, this are some of the analog pins of our Launchpad. Another thing about the analog pins is that, you do not necessarily need to set it up as an input in the void setup(), you can but its optional.
New PRE-WRITTEN FUNCTIONS OF ENERGIA THAT WE ARE GOING TO BE USING:
This functions are already on Energia and are there for us to use it and make our life a little easier:
- analogRead(pin): Reads the value from the specified analog pin and converts the voltage into a value from 0 to 1023 the only difference from digitalWrite is that digitalWrite just reads HIGH or LOW. analogWrite will map input voltages between 0 and ~3 volts (VCC) into integer values between 0 and 1023, 0 volts being 0, 1.5 volts being 512. and 3 volts being 1023.
Ex. analogRead(2)//reading the state of pin 2 which is an analog pin (A0).
digital read most of the time needs to be used with another function, because by itself analogRead, reads the state but it won't do anything or won't let you know what is the state.
Ex2..if ( analogRead(A0)> 512)
{ //do something} // here we say that if the state of the A0 (pin2) is above 512 (1.5v) do something - Serial.begin(9600): To enable our Launchpad to send diagnostic data back to the computer, we need to initialize it using, Serial.begin(9600) in the void setup() function. The number 9600 needs to be used if we are going to use Serial.begin.
- Serial.print ("message"): Prints data to the serial port as human-readable text. This command can take many forms, depending on the purpose.
Different formats (for messages):
-Serial.print (78) gives "78"
-Serial.print (1.25) gives "1.25"
-Serial.print ('N') gives "N"
-Serial.print ("Hello world.") gives "Hello world."
Different formats (for messages in different languages):
-Serial.print (78, BIN) gives "1001110" (78=1001110 in binary)
-Serial.print(78, OCT) gives "116" (78=116 in octal)
-Serial.print(78, HEX) gives "4E" (78=4E in hexadecimal)
-Serial.print(78, DEC) gives "78" (78=78 in decimal)
We can use it to send the reading of certain pin by using an integer.
Ex. int Reading = analogRead(A3);
Serial.print (Reading, BIN) // Here Reading is the value of the reading of pin A3, so we will be getting that value in BINARY language.
In order to use Serial.print we need to have Serial.begin(9600) in the void setup(). - Serial.println: Serial.println is the same thing as Serial.print but it gives an end to go to the next line.
If we want to get the reading of a pin with a message, we would like to have something like: "Reading from pin 5=(actual reading value)."
We need to use Serial.print twice, one for the message "Reading from pin 5 =" and the other one to actually read the pin.
Ex. Int Reading= analogRead(5)
Serial.print("Reading from pin5 =") //The message you are going to get.
Serial.println (Reading, DEC) // The value that is going after the message and will end the line
FINAL PRODUCT: Reading from pin5 = (reading value)
Reading from pin5 = (reading value)
Reading from pin5 = (reading value)
AND ITS GOING TO KEEP GOING AND GOING
If we did not use print ln to finish the line, the result would be:
Reading from pin5 = (reading value) Reading from pin5 = (reading value) Reading from pin5 = (reading value)
All in a single line. You will understand better once you upload the code to the Launchpad.
What is a potentiometer?
In simple words a potentiometer is a resistor with variable resistance. So what is a resistor? a resistor is a something that controls the flow of electricity, the more the resistance the less electricity pass through the resistor. Let's think in the potentiometer as a big resistor, but we can change the resistance of it as we want, just by turning the dial.
Potentiometer as a button.
Step 1:
Set up the hardware, like show in the picture below, just the cable and the potentiometer, do not move any jumper or anything else. Depending on which version of the Launchpad you have, you might need FEMALE/MALE jumper wires or MALE/MALE. I definitely recommend to buy the FEMALE/MALE if you have the rev.1.5 Launchpad, this can be found here
Step 2:
Now that our hardware is all set up, plug your launchpad into your computer and let's go into the software. Open energia and in a new sketch write down the following code:
Step 3:
Can you guess what is going to happen just by reading the code? Go ahead compile it and upload it into your launchpad.
Where your expectations right?. If you turn the dial of the voltimeter all the way to the left the LED should be off, and if you turn it all the way to the right should turn on. Can you figure the exact place to move the voltimeter and it will turn on?.
Now go to the serial monitor button which is the one at the top right corner of energia (looks kind of a magnifying glass) and press it, you should get a new window with constant readings of the voltage going through the voltimeter.
Where your expectations right?. If you turn the dial of the voltimeter all the way to the left the LED should be off, and if you turn it all the way to the right should turn on. Can you figure the exact place to move the voltimeter and it will turn on?.
Now go to the serial monitor button which is the one at the top right corner of energia (looks kind of a magnifying glass) and press it, you should get a new window with constant readings of the voltage going through the voltimeter.
Walk THROUGH the code:
- First thing we do is set an integer "int" which is ledPin= 2, we do this so that in the future if we want to change the LED, we do not need to change it everywhere in the code, and just change it in the integer at the beginning of the code,
- In the void setup() we can see that we set up the ledPin as an output, and we also use a new function which is Serial.begin(9600), we use this function because we are going to send data back to the computer from the potentiometer, and every time we want to send data back to the computer we need to use serial.begin(9600) at the void setup().
- You need to be aware that we did not set up our pin A3 as an input and the Launchpad still is going to get the reading, this is because A3 is an analog pin, and this special pins will work even if you do not set them up as an input, you can do it but is optional.
- Now we pass to the void loop(), first thing to see is that we create another integer "int" which is analogValue and we set it equal to analogRead(A3). So whenever we use analogValue would be the same as as using analogRead(which reads the analog signal of a pin) of pin 5.
- The next thing we see is an if/else statement and is saying that if analogValue (which is analogRead of A3)) reads a value bigger than 512, turn the LED on, and if you remember, an analog signal is between 0 and 1023 in our Launchpad, so >512 pretty much means that, if the dial is more than halfway to the right turn the LED on.
- Otherwise (else)(below 512) turn the LED off.
- At the end we see Serial.print() and Serial.println(), this is because we want to send data back to the computer, and we did initialize it at the setup with Serial.begin(9600), now we are going to tell Energia what data do we actually want send back to the computer.
- With Serial.print() what we did is to set up the message that is going to appear (to set up the message we just nees to put the quotations marks between the message that we want), and with Serial.println (analogValue,DEC) we actually tell Energia what value do we want by using analogValue, which is the same as analogRead(A3), and we want it in decimal format which are the regular numbers.
We could have use it Serial.print again instead of Serial.println and it would have worked but the diference is that when you use Serial.println pretty much tells energia that, that is the last line so whenever we get the value of analogValue, Energia finishes the line and creates a new one and do the same, to get all the different constant readings.
Potentiometer blinking
STeps:
As usual open a new sketch in Energia copy the code below and analyze it, try to figure out what is going to happen. The compile it and upload it to your Launchpad. Where your expectations right?
So what is going on?
By this point you should be able to know what is going on. :D
As you move the dial of the potentiometer analog value is going to have a different value, that is going to be in the range o 0 and 1023, because that is the value of an analog signal in our Launchpad. And if the value of analogValue changes the value of delay changes because the value of delay is analogValue. So depending where the dial of the potentiometer is, is how fast or slow the LED is going to blink.
As you move the dial of the potentiometer analog value is going to have a different value, that is going to be in the range o 0 and 1023, because that is the value of an analog signal in our Launchpad. And if the value of analogValue changes the value of delay changes because the value of delay is analogValue. So depending where the dial of the potentiometer is, is how fast or slow the LED is going to blink.