Netduino home hardware projects downloads community

Jump to content


Photo

Anyone have a good PWM example for Netduino Plus 2?


  • Please log in to reply
11 replies to this topic

#1 Dave VanderWekke

Dave VanderWekke

    Advanced Member

  • Members
  • PipPipPip
  • 857 posts
  • LocationHighland Lakes, New Jersey, USA

Posted 21 November 2012 - 05:08 PM

I've been playing with the ND+2 trying to convert over some projects. I'm throwing errors getting the PWM pins working. Anyone have a good example? I'm using 3 PWM pins (5,6,9) for the RGB LCD Backlight.

Also does anyone have a mapping of the pins and how to define the PWM pins?

If I want to use Digital Pin9 for Red on my RGB is the definition?

PWM _Red  = new PWM(Cpu.PWMChannel.PWM_3, 0, 0, false);

The reason I am using 0 and 0 for the freq and dutycycle is that I am programatically setting them later.

Software Architect, Electronic Engineer, Car Enthusiast, Chef, Blogger, Paranormal Investigator (and Skeptic). I'm like MacGyver, Alton Brown, Doc Brown and Peter Venkman all in one.
My Blog Site: JerseyTechGuy's Blog
Facebook:JerseyTechGuy on Facebook
Twitter: @JerseyTechGuy

My Netduino 2 Tutorials
My Netduino Based Paranormal Investigation Tools
 


#2 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 21 November 2012 - 05:29 PM

Hi Dave,

Here's a quick sample that I have used. Note the use of the PWMChannels instead of the Cpu.PWMChannel. Also I have noticed that it seems that the constructor doesn't like it when you don't have anything set for the Frequency and DutyCycle, so I generally place a value in there and set it afterwards.

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoPlus2_PulseLED
{
    public class Program
    {
        public static void Main()
        {
            PWM led1 = new PWM(PWMChannels.PWM_ONBOARD_LED, 100, .5, false); // Set later
            PWM led2 = new PWM(PWMChannels.PWM_PIN_D5, 100, .5, false); //50% brightness

            led1.Frequency = 100;
            led1.DutyCycle = 1;

            led1.Start();
            led2.Start();
            
            while (true)
            {
                double startValue, endValue;

                for (startValue = 4.712; startValue < 10.995; startValue = startValue + 0.0005)
                {
                    endValue = System.Math.Sin(startValue) * .5 + .5;
                    led1.DutyCycle = endValue;
                    led2.DutyCycle = endValue;
                }
            }
            
        }

    }
}



#3 Dave VanderWekke

Dave VanderWekke

    Advanced Member

  • Members
  • PipPipPip
  • 857 posts
  • LocationHighland Lakes, New Jersey, USA

Posted 21 November 2012 - 05:43 PM

Thanks Gutworks. I missed the Channels vs. Channel part. Explains why I wasn't finding the proper enumeration. Guess I need more coffee. :blink:

Software Architect, Electronic Engineer, Car Enthusiast, Chef, Blogger, Paranormal Investigator (and Skeptic). I'm like MacGyver, Alton Brown, Doc Brown and Peter Venkman all in one.
My Blog Site: JerseyTechGuy's Blog
Facebook:JerseyTechGuy on Facebook
Twitter: @JerseyTechGuy

My Netduino 2 Tutorials
My Netduino Based Paranormal Investigation Tools
 


#4 Wicky

Wicky

    New Member

  • Members
  • Pip
  • 2 posts

Posted 28 November 2012 - 06:11 AM

I have a question guys, I've been having some trouble as well getting used to the new PWM implementation. I managed to brick my Netduino Mini a couple times using guessed sane values that apparently weren't so sane afterall... had to reflash it twice. I think I have a handle on that now... but.. I am working on a project with two servos (a Pan/Tilt platform). Before upgrading from 4.1 I used the old example code from the O'Reilly books, it appears there is no longer a "SetPulse" member function in the PWM class. So the question is... what is the equivalent of "SetPulse" with the 4.2.x PWM class? Any ideas?

#5 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7142 posts
  • LocationNew York, NY

Posted 30 November 2012 - 09:11 AM

Hi Wicky,

So the question is... what is the equivalent of "SetPulse" with the 4.2.x PWM class?

If you're using a Netduino or Netduino Plus 1, you can actually pull in the SecretLabs.NETMF.Hardware.PWM.dll class and use the existing code.

And to answer your specific question, here is what the new SetPulse function does internally:
       public void SetPulse(UInt32 period, UInt32 duration)
        {
            _pwm.Period = period;
            _pwm.Duration = duration;
        }
Basically it just sets the PWM period and duration using the two values you pass in. You should be able to break out your existing code to two lines...and it should work well for you.

Chris

#6 ricafort

ricafort

    New Member

  • Members
  • Pip
  • 5 posts

Posted 13 March 2013 - 09:20 AM

I cannot find setpulse() function on netduino 2. how can I run the code below in netduino 2?

 

Making Music example

 

public static void Main()
{
// write your code here
// store the notes on the music scale and their associated pulse lengths
System.Collections.Hashtable scale = new System.Collections.Hashtable();
// low octave
scale.Add("c", 1915u);
scale.Add("d", 1700u);
scale.Add("e", 1519u);
scale.Add("f", 1432u);
scale.Add("g", 1275u);
scale.Add("a", 1136u);
scale.Add("b", 1014u);
// high octave
scale.Add("C", 956u);
scale.Add("D", 851u);
scale.Add("E", 758u);
// silence ("hold note")
scale.Add("h", 0u);
int beatsPerMinute = 90;
int beatTimeInMilliseconds =
60000 / beatsPerMinute; // 60,000 milliseconds per minute
int pauseTimeInMilliseconds = (int)(beatTimeInMilliseconds * 0.1);
// define the song (letter of note followed by length of note)
string song = "C1C1C1g1a1a1g2E1E1D1D1C2";
// define the speaker
PWM speaker = new PWM(Pins.GPIO_PIN_D5);
// interpret and play the song
for (int i = 0; i < song.Length; i += 2)
{
// extract each note and its length in beats
string note = song.Substring(i, 1);
int beatCount = int.Parse(song.Substring(i + 1, 1));
// look up the note duration (in microseconds)
uint noteDuration = (uint)scale[note];
// play the note for the desired number of beats
speaker.SetPulse(noteDuration * 2, noteDuration);
Thread.Sleep(
beatTimeInMilliseconds * beatCount - pauseTimeInMilliseconds);
// pause for 1/10th of a beat in between every note.
speaker.SetDutyCycle(0);
Thread.Sleep(pauseTimeInMilliseconds);
}
Thread.Sleep(Timeout.Infinite);
}


#7 ricafort

ricafort

    New Member

  • Members
  • Pip
  • 5 posts

Posted 13 March 2013 - 11:05 AM

I got it working now using the solution below:

 

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]"If you're using AnalogInput, add the SecretLabs.NETMF.Hardware.AnalogInput.dll assembly as a reference to your project. If you're using PWM, add the SecretLabs.NETMF.Hardware.PWM.dll assembly as a reference to your project."[/color]

 

http://forums.netdui...uino-2/?p=47140



#8 Ken Baker

Ken Baker

    New Member

  • Members
  • Pip
  • 2 posts
  • LocationCincinnati, Ohio

Posted 25 March 2013 - 08:49 PM

I cannot find setpulse() function on netduino 2. how can I run the code below in netduino 2?

 

Making Music example

 

public static void Main()
{
// write your code here
// store the notes on the music scale and their associated pulse lengths
System.Collections.Hashtable scale = new System.Collections.Hashtable();
// low octave
scale.Add("c", 1915u);
scale.Add("d", 1700u);
scale.Add("e", 1519u);
scale.Add("f", 1432u);
scale.Add("g", 1275u);
scale.Add("a", 1136u);
scale.Add("b", 1014u);
// high octave
scale.Add("C", 956u);
scale.Add("D", 851u);
scale.Add("E", 758u);
// silence ("hold note")
scale.Add("h", 0u);
int beatsPerMinute = 90;
int beatTimeInMilliseconds =
60000 / beatsPerMinute; // 60,000 milliseconds per minute
int pauseTimeInMilliseconds = (int)(beatTimeInMilliseconds * 0.1);
// define the song (letter of note followed by length of note)
string song = "C1C1C1g1a1a1g2E1E1D1D1C2";
// define the speaker
PWM speaker = new PWM(Pins.GPIO_PIN_D5);
// interpret and play the song
for (int i = 0; i < song.Length; i += 2)
{
// extract each note and its length in beats
string note = song.Substring(i, 1);
int beatCount = int.Parse(song.Substring(i + 1, 1));
// look up the note duration (in microseconds)
uint noteDuration = (uint)scale[note];
// play the note for the desired number of beats
speaker.SetPulse(noteDuration * 2, noteDuration);
Thread.Sleep(
beatTimeInMilliseconds * beatCount - pauseTimeInMilliseconds);
// pause for 1/10th of a beat in between every note.
speaker.SetDutyCycle(0);
Thread.Sleep(pauseTimeInMilliseconds);
}
Thread.Sleep(Timeout.Infinite);
}

Is there actually a updated version of this code (which comes from the "Getting Started with Netduino" book). I'd like to see it as I cannot find the proper documentation for what the PWM arguments actually mean in Netduino land. Specifically what the first argument is. There are different channels, but no explanation as to which is what that I can find.

 

Thanks! Ken



#9 Leon

Leon

    New Member

  • Members
  • Pip
  • 4 posts

Posted 27 March 2013 - 02:59 PM

my visual studio cannot run follow codes:PWM pwm = new PWM(Pins.GPIO_PIN_D5);  and  pwm.setDutyCycle(30);

who can tell me why ?  thank the gods...



#10 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7142 posts
  • LocationNew York, NY

Posted 30 March 2013 - 05:38 PM

Hi Ken,

Is there actually a updated version of this code (which comes from the "Getting Started with Netduino" book). I'd like to see it as I cannot find the proper documentation for what the PWM arguments actually mean in Netduino land. Specifically what the first argument is. There are different channels, but no explanation as to which is what that I can find.

With the new PWM Channels, you can identify which one goes to which pin by using the "PWMChannels." enumeration (PWMChannels.PWM_PIN_D#). We'll be updating the book samples soon to use the new NETMF 4.2+ classes. Is that what you needed? Chris

#11 Ken Baker

Ken Baker

    New Member

  • Members
  • Pip
  • 2 posts
  • LocationCincinnati, Ohio

Posted 29 April 2013 - 07:40 PM

Have the updates for the book samples made it out on the internet or here in the community forums somewhere? Looking at using this as an idea for introductory IT classes as it contains a little of everything IT folks end up touching and doing things with.

 

Thanks!

Ken



#12 c2rosa

c2rosa

    Member

  • Members
  • PipPip
  • 15 posts

Posted 04 June 2014 - 05:12 PM

Hi Wicky,

If you're using a Netduino or Netduino Plus 1, you can actually pull in the SecretLabs.NETMF.Hardware.PWM.dll class and use the existing code.

And to answer your specific question, here is what the new SetPulse function does internally:

       public void SetPulse(UInt32 period, UInt32 duration)
        {
            _pwm.Period = period;
            _pwm.Duration = duration;
        }
Basically it just sets the PWM period and duration using the two values you pass in. You should be able to break out your existing code to two lines...and it should work well for you.

Chris

 

Chris,

And when you update the fields Period and Duration as you've shown in the code snippet above, the change happens immediately and automatically?  You don't have to do anything to get the changes to "take"?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2010-2014 Secret Labs LLC  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.