Shop OBEX P1 Docs P2 Docs Learn Events
Use of Rotary Encoder and RGB LED module — Parallax Forums

Use of Rotary Encoder and RGB LED module

I need some help with this project. I have an WX Activity board/Robot and I need to write a program in Blockly Prop using the Rotary Encoder as an input and show proof that the sensor is outputting the button push and that the knob was turned, so terminal would be ok. I wanted to up the ante and add the RGB module in the mix and make it so as you turn the knob, the color of the LED changes. Can anyone point me in the right direction on how to program this?


  • Have you seen these tutorials ? The RGB module in included near the bottom of the list.

    For the encoder... you could treat that like a switch input? Maybe have a Function running in it's own cog that checks all the input wires from the encoder in a loop, and each time the encoder signal state changes from the last, then increment a variable that keeps count for you to use elsewhere in your code. (You could just display the current count value in the debug terminal, for example). As for the switch, similar thing, except instead of incrementing a counter, you might just have a variable that is 0 or 1 depending on if the switch is pressed or not. Again, you could display that in your code. Or make the RGB flash when the switch is pressed, etc...

    At the same link above you'll find "Functions and Multicore" tutorial, and also the "4x4 Keypad " might help to figure out how to code for the encoder inputs.

    Give those a try and experiment- let us know how you get on, or if you need more help.

  • which pins would you plug the 3 other wires (CLK, DT, SW) i think i figured out power and gnd.

  • well i figured out that SW is the button switch in the encoder and CLK and DT are encoder A and B respectfully. so I imagine that all 3 pins can go in the P0-P15 pins.

  • Correct, essentially SW, CLK and DT all behave like switches. So in your code you'd read them the same way. Just hook them into P0 to P15 range as you suggest.

    Other tips...

    If your rotary encoder doesn't include the pull-up resistors, then you will likely need to add a 10K pull-up resistor from each IO pin to 3.3V.
    As you are using the Propeller which has 3.3V IO pins, then hook the Rotary encoder + pin to 3.3V (instead of the 5V that the encoder might have printed on it!)

    If you are using this Parallax item, the pull-up resistors are already installed for you on the back of the encoder PCB.

  • Attached file might be useful...

  • I ended up taking a short trip to Parallax on Friday and had a great time. I picked up 2 encoders, got an impromptu tour of the place and had a nice chat. It was really neat! So now i have the encoders and I want to program it with using blocklyprop. I have a dupont cable from the encoder to the Activity Board with gnd and voltage taken care of. I plugged the remaining 3 to pins 4,5, and 6. there also 2 LEDs on the breadboard on pins 15 and 0 respectfully. what i want to do is, if the encoder rotates CW, the LED on the right lights up, and the opposite if CCW. im having a problem setting this up, and maybe im just overthinking it but it shouldnt be this hard to make an LED light up if you turn the knob.

    can someone point me in the right direction?

  • Nice! Hadn't realised Parallax Rocklin were able to take guests around the office again. Last I saw the building was massively ripped out and being refurbed from top to bottom, with a new training center in the middle... You might have been one of the first to see it!

    For your code, if you refer to that PDF above you'll see the logic about how to read the direction.

    Looks like you could have a loop with 3 variables, perhaps called A, B and SW. Then read the state of the three pins into those three variables.
    After that check the values...

    if SW is 0 then the switch is pressed.
    if A is 0 and B is 1 then the encoder is being turned one way
    if A is 1 and B is 0 then the encoder is being turned the other way
    if A and B are other values then ignore for now - just continue your loop. (Although you might respond to these other values later, to determine that the encoder is not currently being turned, perhaps if they don't change for a certain time period).

    After you check all the values, and perform any actions you want (like turn on or off your LEDs), then your loop repeats and you keep checking the inputs to see if they changed.

    That could be a good starting point to experiment. Later, you might find you want to add some little delays in the loop, and/or add additional variables (such as: A_prev, B_prev, SW_prev) to examine the previously known state as well as the new input start of each input. Possibly to de-bounce the transitions or to stop the LEDs flashing madly :) You'll see as you go- best to start with a basic loop and experiment from there to figure out what extra layers of logic you want to add to fine tune the performance to what you need.

Sign In or Register to comment.