Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    GPIO Pins on DUEX5 Useage

    Scheduled Pinned Locked Moved
    Duet Hardware and wiring
    4
    11
    803
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • wkellyoundefined
      wkellyo
      last edited by

      I use GPIO Pin1 on the DUEX5 to read a PWM value of the various functions of the printer using this Arduino Code which triggers NeoPixel LEDs for Bed Lights using macros and Gcode in the printer files. (I didn't write it A1r did).

      My question is what is the min and max range of the PWM Values? These are millisecond values? I attempted to add another color using 625 -<675 as the PWM value and it didn't work. Is 575 as high as I can go? My added code is bolded below in the working code, please scroll down to see. I'm not asking for Arduino Coding just what the PWM range is.

      The code below works:
      // Command to change led scenarios: M42 P0 Sx (x in multiples of 10. Using GPIO Pin 1 on Duex5 Board)
      #include <Adafruit_NeoPixel.h>
      #define PIN 6
      #define delayLED 5
      #define StandbyBrightness 30
      #define LowBrightness 75
      #define MidBrightness 127
      #define HighBrightness 255
      #define LED_COUNT 90

      Adafruit_NeoPixel strip = Adafruit_NeoPixel (LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);
      // Read pin, connected to pin P0 (GPIO1 on DUEX5) and duex.gp1 for RRF3
      byte PWM_PIN = A2;
      int Value_pwm;

      void setup () {
      // Start LED strip
      pinMode (PWM_PIN, INPUT);
      Serial.begin (115200);
      strip.begin ();
      strip.setBrightness (255);

      // Initialize the strip
      strip.show ();

      // Start orange sequence
      for (int i = 0; i <LED_COUNT; i ++) {

      strip.setPixelColor (i, strip.Color (255,155,0));
      strip.show ();
      delay (delayLED);
      }
      }

      void loop () {
      // Read the value of PWM_PIN and set it to Value_pwm
      Value_pwm = pulseIn (PWM_PIN, HIGH);

      Serial.println (Value_pwm); // Submit the value to monitor sampled values

      // comparison of stock strips to have sufficient clearance between stripe and stripe

      if (Value_pwm> -1 && Value_pwm <100) {Serial.println (Value_pwm); Serial.println ("Sleep"); Standby ();} // M42 P0 S09 in Duet
      if (Value_pwm> 125 && Value_pwm <175) {Serial.println (Value_pwm); Serial.println ("White"); White ();} // M42 P0 S20 in the Duet
      if (Value_pwm> 200 && Value_pwm <250) {Serial.println (Value_pwm); Serial.println ("Red"); Red ();} // M42 P0 S29 in the Duet
      if (Value_pwm> 275 && Value_pwm <325) {Serial.println (Value_pwm); Serial.println ("Green"); Green ();} // M42 P0 S39 in the Duet
      if (Value_pwm> 350 && Value_pwm <400) {Serial.println (Value_pwm); Serial.println ("Blue"); Blue ();} // M42 P0 S49 in the Duet
      if (Value_pwm> 425 && Value_pwm <475) {Serial.println (Value_pwm); Serial.println ("Off"); Off ();} // M42 P0 S58 in the Duet
      if (Value_pwm> 525 && Value_pwm <575) {Serial.println (Value_pwm); Serial.println ("On"); On ();} // M42 P0 S68 in the Duet

      I added this under the above using S79 as the trigger. There is also another bit of code at the very bottom, scroll down:
      // if (Value_pwm> 625 && Value_pwm <675) {Serial.println (Value_pwm); Serial.println ("Purple"); Purple ();} // M42 P0 S79 in the Duet
      }
      void Standby () {
      // Sequentially put all the LEDs on Standby
      strip.setBrightness (StandbyBrightness);
      for (int i = 1; i <LED_COUNT; i ++) {
      strip.setPixelColor (i, strip.Color (50,50,50));
      strip.show ();
      delay (delayLED);
      }
      while (Value_pwm> -1 && Value_pwm <100)
      {Value_pwm = pulseIn (PWM_PIN, HIGH);
      }
      }
      void White () {
      // Sequentially set all LEDs to White
      strip.setBrightness (HighBrightness);
      for (int i = 0; i <LED_COUNT; i ++) {
      strip.setPixelColor (i, strip.Color (255,255,255));
      strip.show ();
      delay (delayLED);
      }
      while (Value_pwm> 125 && Value_pwm <175)
      {Value_pwm = pulseIn (PWM_PIN, HIGH);
      }
      }
      void Red () {
      // Set all LEDs sequentially to Red
      strip.setBrightness (MidBrightness);
      for (int i = 0; i <LED_COUNT; i ++) {
      strip.setPixelColor (i, strip.Color (255,0,0));
      strip.show ();
      delay (delayLED);
      }
      while (Value_pwm> 200 && Value_pwm <250)
      {Value_pwm = pulseIn (PWM_PIN, HIGH);
      }
      }
      void Green () {
      // Sequentially turn all LEDs to Green
      strip.setBrightness (MidBrightness);
      for (int i = 0; i <LED_COUNT; i ++) {
      strip.setPixelColor (i, strip.Color (0,255,0));
      strip.show ();
      delay (delayLED);
      }
      while (Value_pwm> 275 && Value_pwm <325)
      {Value_pwm = pulseIn (PWM_PIN, HIGH);
      }
      }
      void Blue () {
      // Sequentially put all the LEDs to Blue
      strip.setBrightness (MidBrightness);
      for (int i = 0; i <LED_COUNT; i ++) {
      strip.setPixelColor (i, strip.Color (0,0,255));
      strip.show ();
      delay (delayLED);
      }
      while (Value_pwm> 350 && Value_pwm <400)
      {Value_pwm = pulseIn (PWM_PIN, HIGH);
      }
      }
      void Off () {
      // Turn off all LEDs sequentially
      strip.setBrightness (0);
      for (int i = 0; i <LED_COUNT; i ++) {
      strip.setPixelColor (i, strip.Color (0,0,0));
      strip.show ();
      delay (delayLED);
      }
      strip.clear ();
      while (Value_pwm> 425 && Value_pwm <475)
      {Value_pwm = pulseIn (PWM_PIN, HIGH);
      }
      }
      void On () {
      // Sequentially turn on all the LEDs
      strip.setBrightness (MidBrightness);
      for (int i = 0; i <LED_COUNT; i ++) {
      strip.setPixelColor (i, strip.Color (255,128,0));
      strip.show ();
      delay (delayLED);
      }
      while (Value_pwm> 525 && Value_pwm <575)
      {Value_pwm = pulseIn (PWM_PIN, HIGH);
      }
      }

      My code was added end of the sketch.

      void Purple () {
      // Turns all LEDs sequentially
      strip.setBrightness (0);
      for (int i = 0; i <LED_COUNT; i ++) {
      strip.setPixelColor (i, strip.Color (255,0,255));
      strip.show ();
      delay (delayLED);
      }
      strip.clear ();
      while (Value_pwm> 625 && Value_pwm <675)
      {Value_pwm = pulseIn (PWM_PIN, HIGH);
      }
      }

      dc42undefined 1 Reply Last reply Reply Quote 0
      • dc42undefined
        dc42 administrators @wkellyo
        last edited by dc42

        You can't read a PWM value on a GPIO pin except by timing the high and low transitions and doing some calculations from those times; or by smoothing the PWM signal (e.g. using a RC filter) and feeding the resulting voltage onto an analog input. You would need to modify the firmware to do that.

        If you control the PWM value, why do you need to read it anyway?

        Duet WiFi hardware designer and firmware engineer
        Please do not ask me for Duet support via PM or email, use the forum
        http://www.escher3d.com, https://miscsolutions.wordpress.com

        1 Reply Last reply Reply Quote 0
        • wkellyoundefined
          wkellyo
          last edited by

          What I have is GPIO1 of the Duex5 connected to Analog PIN "A2" of an Arduino Pro Mini. In Config.g, I have this to enable the GPIO pin: M950 P0 C"duex.gp1"

          I have it all working and can change the addressable LED Lighting colors using the G Codes shown in the sketch code. I use the M42 command such as M42 P0 S20 which changes the LEDS to white. I've added Gcodes to some of the system files that run and the lights change depending on what the printer is doing.

          Since I'm not a programmer, I'm not exactly sure what the code is doing when it references: if (Value_pwm> 200 && Value_pwm <250) but just guessing, its reading the PWM value that changes when sending the M42 command.

          I was wondering how high I can go with the if (Value_pwm> xxx && Value_pwm <xxx) before exceeding the limits of the Duet System. As my earlier message said, I tried: if (Value_pwm> 625 && Value_pwm <675) without success. Either I goofed the code or sending the M42 P0 S79 didn't do anything. I also tried S73,4,5,6,7,8, and a few after that without success.

          1 Reply Last reply Reply Quote 0
          • Danalundefined
            Danal
            last edited by Danal

            This post is deleted!
            wkellyoundefined 1 Reply Last reply Reply Quote 0
            • wkellyoundefined
              wkellyo @Danal
              last edited by

              @Danal, Yes, it's a 5v pro mini and it's a one way direction from DUEX5 GPIO1 to Pin A2 Of the Pro Mini. I do have a 3.3 to 5v logic level converter device, I should put it in between? If I got a 3.3 v Pro Mini, I'd have more room to add PWM values?
              Bottom line, I'm fairly new to Arduino and am trying to learn code. If you look at the first post of the code I've commented what each GCode does, I copied it in again below and bolded the GCodes and Colors. After I got it all working I thought I would try adding another color Purple. That code is bolded in the first post.

              What you are saying indicates that my guess of 625-675 is above the range. If you study the code I was trying to add (again just copying, pasting and altering the PWM in the same way the other codes are) It won't work. But, if I lower the PWM to say 600-620 or so It might work? If I tighten the ranges of the other colors I could also add more, keeping the last one under 624? I suppose if it's too tight, the arduino may not see the change? I studied the G42 write up but it wasn't clear enough for me. Thank you so much for replying, what you have said has helped me understand this even more.

              if (Value_pwm> -1 && Value_pwm <100) {Serial.println (Value_pwm); Serial.println ("Sleep"); Standby ();} // M42 P0 S09 in Duet
              if (Value_pwm> 125 && Value_pwm <175) {Serial.println (Value_pwm); Serial.println ("White"); White ();} // M42 P0 S20 in the Duet
              if (Value_pwm> 200 && Value_pwm <250) {Serial.println (Value_pwm); Serial.println ("Red"); Red ();} // M42 P0 S29 in the Duet
              if (Value_pwm> 275 && Value_pwm <325) {Serial.println (Value_pwm); Serial.println ("Green"); Green ();} // M42 P0 S39 in the Duet
              if (Value_pwm> 350 && Value_pwm <400) {Serial.println (Value_pwm); Serial.println ("Blue"); Blue ();} // M42 P0 S49 in the Duet
              if (Value_pwm> 425 && Value_pwm <475) {Serial.println (Value_pwm); Serial.println ("Off"); Off ();} // M42 P0 S58 in the Duet
              if (Value_pwm> 525 && Value_pwm <575) {Serial.println (Value_pwm); Serial.println ("On"); On ();} // M42 P0 S68 in the Duet

              1 Reply Last reply Reply Quote 0
              • Danalundefined
                Danal
                last edited by Danal

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • Danalundefined
                  Danal
                  last edited by Danal

                  @wkellyo said in GPIO Pins on DUEX5 Useage:

                  // Read the value of PWM_PIN and set it to Value_pwm
                  Value_pwm = pulseIn (PWM_PIN, HIGH);

                  WAIT!!! I just noticed that the Arduino sketch is using PulseIn to read A0. I thought it was using analogRead()... it is not.

                  Therefore ALL of my comments about voltages and ranges GO AWAY. There will be no difference between a 5V and 3.3V Pro Mini. The range will not be 1024*(3.3/5), etc, etc. I've actually deleted a couple of posts above, to avoid confusion:

                  The following things are still true:

                  • When using a 5V pro mini, NEVER connect one of its outputs to the Duet; that will harm the Duet. Going the other way, Duet output to Arduino input, is OK-ish with a lot of caveats.
                  • A level converter will not really help anything in this situation. 3.3V logic output generally switches 5V input cleanly.

                  And:

                  • Do set up the serial monitor in Arduino, at 115200
                  • Do use the Duet web console to enter a series of M42 P0 Snn commands, with incrementing S values.

                  Use the information you learn from this to decide what M42 commands relate to what numbers on the Arduino, and therefore what ranges you wish to use.

                  wkellyoundefined 1 Reply Last reply Reply Quote 0
                  • wkellyoundefined
                    wkellyo @Danal
                    last edited by

                    @Danal Again, thanks for taking the time to help me. I've read about using the serial monitor, guess it's time to dig in and learn how to use it. I will be changing the Arduino Pin to A3 soon so I can use a 4 Pin shell housing to go from GND, VCC, skip, A3 with one plug. If you would like to see the printer in action Homing watch for the bed leds being in the Sleep mode then going full green while it's homing. After homing, they turn back to sleep. As soon as I get this ironed out, I'll be adding the GCodes to the files as we discussed in macro post. In the video you can see the light change for every direction due to having a "homeall.g file with nothing but pointers to the other homing files. I've rewritten the homeall.g with it's own code so the lights are on during the entire operation. Just looks cleaner to me.

                    NeoPixel Bed Lights

                    1 Reply Last reply Reply Quote 0
                    • wkellyoundefined
                      wkellyo
                      last edited by

                      For anyone following this I got the Purple color to work perfectly and was sooo close

                      if (Value_pwm> 625 && Value_pwm <675) {Serial.println (Value_pwm); Serial.println ("Purple"); Purple ();} // M42 P0 S81 in the Duet

                      This code worked! I originally had an S79 but after taking @Danal advice I got the serial monitor working and studied it and found the PWM value was just a bit short of working. S81 in the code did the trick!

                      1 Reply Last reply Reply Quote 0
                      • Danalundefined
                        Danal
                        last edited by

                        Wooo Hooo!

                        1 Reply Last reply Reply Quote 0
                        • matej1006undefined
                          matej1006
                          last edited by

                          @wkellyo how it's look your config.g for this application with arduino?

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post
                          Unless otherwise noted, all forum content is licensed under CC-BY-SA