Pages: [1]   Go Down
Author Topic: Sending voltages to PWM pins through the Serial Monitor  (Read 610 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I am new to Arduino and have been following along through Simon Monk’s book “Programming Arduino Getting Started with Sketches”. The book is excellent, and up until now I have had no problems. However, I am now trying to get sketch 6-08 to work with my Uno. The goal of this sketch is to use PWM pins to generate a voltage between 0 and 5 V. The sketch is shown below:

Code:
//sketch 06-08
int outputPin = 3;
void setup()
{
  pinMode(outputPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Enter Volts 0 to 5");
}
void loop()
{
  if (Serial.available() > 0)
  {
    char ch = Serial.read();
    int volts = (ch - '0') * 51;
    analogWrite(outputPin, volts);
  }
}

When I run the sketch, my Arduino Uno does not generate the voltage it should at the specified pin (in this case, pin 3). Regardless of the voltage I enter into the Serial Monitor, I only measure a voltage of ~ 0.13 V at pin 3 (vs. ground). The same is true for all of the PWM pins (after changing the code to correspond to the new pin, of course).

To determine if this was problem with the code, or a problem with my board, I tried editing the code to look as follows:

Code:
//sketch 06-08
int outputPin = 3;
void setup()
{
  pinMode(outputPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Enter Volts 0 to 5");
  analogWrite(outputPin, 150);
}
void loop()
{

}

By doing this, and by directly varying the voltage of pin 3 in the void setup(), I have verified that my Arduino Uno is working properly (a value in the analogWrite function of 150 results in a voltage of ~ 2.94 V, as expected).

One final clue to this problem comes by adding the lines Serial.println(ch) and Serial.println(volts) into the void loop() of the original code. When I do this, I can see that when I enter a voltage into the Serial Monitor, the variable “volts” initially takes the correct value, but immediately after this it reverts to a value of -1785 regardless of the voltage that I enter into the Serial Monitor. In fact, even if I enter nothing into the Serial Monitor, and simply press “send” the variable “volts” again takes a value of -1785. Based on this I can clearly see that there must be something wrong with the code, but as I am new to this area, I cannot identify the problem.

Specifically, I would like to know:

1) Why does the variable "volts" immediately revert to -1785 regardless of the voltage I enter?
2) How can I modify the code to avoid this problem?

Thanks!

Dustin
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19334
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Regardless of the voltage I enter into the Serial Monitor,

What voltage do you enter? Give an example.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27425
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The values you receive over serial can be seen here
http://www.asciitable.com/

'0' = 48 decimal,
'1' = 49 decimal,
etc.
So you could change this from
    int volts = (ch - '0') * 51;
to
byte volts = (ch - 48) * 51;  // results in 0, 51,102, 153, 204, 255
which is what analogWrite is expecting, an 8-bit number.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19334
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's a clue:

Code:
(13 - 48) * 51 = -1785
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Nick,

Thank you for your fast reply.

I am entering voltages between 0 and 5. I can see how the code is supposed to convert these user defined voltages to values between 0 and 255 to be fed into the analogWrite command. However, it does not seem to be working.

Dustin
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19334
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Say you entered: 5 <carriage-return>

The 5 is processed OK, but carriage-return has the value 13, see my arithmetic above.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19334
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.gammon.com.au/serial
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19334
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

-1785 is F907 in hex. Take the low-order byte (07) and do this math:

Code:
07 / 255 * 5 = 0.13725490196078

That's why you got a voltage of 0.13 volts!

So, everything is working. smiley-wink
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Nick and CrossRoads,

Thank you both for your amazingly fast replies. I really appreciate the help and the explanation. I will try editing my code with this in mind. I wonder why/how this code was able to work for Simon in his book. It specifically states that you should be able to simply enter in the numeric value you desire to achieve at the PWM pin, and it will be generated using this exact code. There is even a picture of a multimeter measuring a value of "3" after "3" is entered into the Serial Monitor...

Anyways, thanks both for your help.

Dustin
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19334
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

He may conceivably have his serial monitor configured to not send anything when you hit Send, rather than carriage-return.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Pages: [1]   Go Up
Jump to: