Netduino home hardware projects downloads community

Jump to content


Photo

How do I properly use PWM?

Netduino Mini PWM

  • Please log in to reply
No replies to this topic

#1 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 30 September 2014 - 10:55 PM

Hello, 

 

I'm trying to use a PWM output to drive a solid state relay. The output turns the fan on, but does not seem to control the speed or ever turn off... 

 

What am I doing wrong? I've got a fresh out-of-the-box Netduino Mini.

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoMini;

//R0.0 - Base Project (Nevin Steinbrink 7/25/2014)
//R0.1 - Added PWM motor control functionality (Nevin Steinbrink 8/2/2014)
//R0.2 - Added third Program Select input, changed Status Pointer configuration (Nevin Steinbrink 9/21/2014)
//R0.3 - Debug (Nevin Steinbrink 9/27/2014)

namespace NetduinoMiniApplication1
{
    public class ICE_Box
    {
        public static int StatusPointer; //Used for Status LED flash pattern

        public static void Main()
        {
            Thread a = new Thread(Status); //Thread for Status LED control
            a.Start();
            Thread b = new Thread(BlowerControl); //Thread for Blower Motor control
            b.Start();

            while (true)
            {
                Thread.Sleep(Timeout.Infinite);
            }
        }

        static void BlowerControl()
        {
            InputPort StartButton = new InputPort(Pins.GPIO_PIN_5, true, Port.ResistorMode.PullUp); //Start button input
            InputPort PS_1 = new InputPort(Pins.GPIO_PIN_6, true, Port.ResistorMode.PullUp); //Program select 1 input, 1 Liter
            InputPort PS_2 = new InputPort(Pins.GPIO_PIN_7, true, Port.ResistorMode.PullUp); //Program select 2 input, 3 Liter
            InputPort PS_3 = new InputPort(Pins.GPIO_PIN_8, true, Port.ResistorMode.PullUp); //Program select 3 input, 5 Liter

            SecretLabs.NETMF.Hardware.PWM BlowerOutput = new PWM(Pins.GPIO_PIN_17); //PWM output for blower relay

            bool PS_1State = true; //Program select 1, initialize as HIGH
            bool PS_2State = true; //Program select 2, initialize as HIGH
            bool PS_3State = true; //Program select 3, initialize as HIGH
            bool StartButtonState = true; //Start button, initialize as LOW
            bool StartLatch = false;
            bool BlowerRunning = false;

            int RunTime = 0; //Blower ON time in ms
            int PWMDutyCycle = 0; //In percent, initialize at 0%

            uint PWMFreq = 400; //In Hz <-- Adjust this value
            uint PWMPeriod;
            uint PWMPulseWidth; //In microseconds

            PWMPeriod = 2500; //((1 / PWMFreq) * 1000000); //In microseconds
            StatusPointer = 1; //Ready LED indication

            while (true)
            {
                PS_1State = PS_1.Read(); //Read state of Program Select switch 
                PS_2State = PS_2.Read(); //Read state of Program Select switch
                PS_3State = PS_3.Read(); //Read state of Program Select switch
                StartButtonState = StartButton.Read(); //Read state of Start button 

                if (StartButtonState == false) //Check if Start button is pressed
                {
                    StartLatch = true; //Start button debounce
                }

                if (PS_1State == false) //Check which program is selected, Program 1 selected
                {
                    RunTime = 5000; //Blower ON for 5 seconds
                    PWMDutyCycle = 10; //Blower at 50% power
                }

                if (PS_2State == false) //Check which program is selected, Program 2 selected
                {
                    RunTime = 8000; //Blower ON for 8 seconds
                    PWMDutyCycle = 30; //Blower at 90% power
                }

                if (PS_3State == false) //Check which program is selected, Program 3 selected
                {
                    RunTime = 10000; //Blower ON for 10 seconds
                    PWMDutyCycle = 100; //Blower at 90% power
                }

                if (PS_1State & PS_2State & PS_3State == false | PS_1State & PS_2State & PS_3State == true)
                {
                    StatusPointer = 4;
                }

                while (StartLatch == true & BlowerRunning == false) //Check if OK to start and blower is currently OFF
                {
                    PWMPulseWidth = 250; //(uint)(PWMPeriod * (PWMDutyCycle / 100));
                    BlowerRunning = true; //Prevents re-entering while loop
                    StatusPointer = 2; //Changes LED indication to 'in operation'
                    BlowerOutput.SetPulse(PWMPeriod, PWMPulseWidth); //Turn on Blower at set PWM
                    Thread.Sleep(RunTime); //For this long
                    BlowerOutput.SetPulse(0, 0); //Turn off Blower
                    StartLatch = false; //Reset StartLatch
                    BlowerRunning = false; //Reset BlowerRunning
                    StatusPointer = 3; //Changes LED indication to 'completed'
                }
            }
        }

I've commented out some stuff to help debug, but no help. 

 

Thanks!!

-Nevin







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

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