This tutorial uses a pre-built node group to encapsulate a 5 pin rotary encoder. A custom Arduino node wires up 2 interrupt routines to count ticks. The amount of ticks per rotation can be set, and roll over can be turned on and off at run time.

Here is the custom Arduino node used in this tutorial.

View Comments (0)

I’ve got a 5 pin rotary encoder that came with the 37 in 1 sensor kit. A rotary encoder is like a potentiometer, except it doesn’t have a physical limit and keeps turning.

I’m using an Arduino Nano for this. First I plug the encoder into my bread board. I’ll plug in the pins from left to right. The first pin connects to ground, and the second connects to the voltage channel. The middle pin gives a signal when the encoder knob is pressed in like a button, I’ll connect it to digital pin 4. The last two pins are used for the rotation signals. The code we’ll use uses interrupts, so I plug these into digital pins 2 and 3 which are the interrupt enabled pins on the Nano.

In an Embrio project I rename the default agent Rotary Encoder. There’s a pre group of nodes in the library. I find it in the library tree. As of the making of this video, it’s in Inputs, Encoders, 5 Pin Rotary Encoder. The library is maintained dynamically on the web site, so when you’re watching this video it could have been moved. To make a copy of the pre-made node group, just drag it onto the node screen. Or you can find it on the web site and copy and paste the XML.

Before going over the details, I’ll show you how its used. You shouldn’t add any more nodes to this agent unless they are specifically related to the encoder hardware. Remember that in Embrio you want to encapsulate everything with an agent and keep things cleanly separated that way. I’ll make a new agent and drag the Rotary Encoder agent onto it. Now we have a clean node that only shows us the interesting inputs and outputs. When I’m connected to the Arduino, I can turn the knob and see the Activation change. By default the value is clamped to 0 and 1 and doesn’t roll over, even if I keep turning the knob. The agent has a Rollover input. If you set it to something above .5, it turns on rollover, and now when the knob is turned past the end value, the activation starts over and a trigger fires. The agent also has a Set Activation and trigger input. When the trigger is fired, the output activation is reset to the input. On the encoder I’m using there’s a built in button. If you push down on it, the clicked trigger fires and the activation is reset.

The Rotary Encoder agent is ready to use in your projects. The rest of this tutorial will go further into the custom Arduino node.

I’ll start with the declaration section. First the variables used in the node are defined. The pins being used are set as constant values. If you’re using a Nano these will have to be 2 and 3, if you’re using another Arduino look up which pins are interrupt enabled. Next is a value that records the current position of the encoder. Next are two variables that define the range and behavior of the encoder. The encoderMax variable defines how many ticks are in a full turn. My encoder has 20 ticks, but you can set this to just about whatever you like. Next is a boolean value that defines if the value rolls over or not. When the value is false, the activation output will clamp and 0 and 1, when it’s true it will reset when it reaches an end. This value is set by the Rollover input activation, which we’ll get to soon. After this are two functions, one for each interrupt. These functions just check if the pin value changed, and update the encoder position if they did.

The setup code block is pretty simple, it just sets the pin mode on the interrupt pins and wires up the interrupt functions.

The first dynamic code block is set to process on every update. It simply transforms the internal encoder position variable to the output activation range of 0 to 1.

The next code block fires when the input trigger fires. All it does is set the internal encoder position based on the input set activation.

Finally there is an input change code block, which executes when the input Rollover activation changes. All this code does is set the internal rollover flag to true if the input is greater than .5 or false if it is less.

In addition to the custom Arduino node, there are two more nodes. A digital input reads the value of the push button that’s built into the encoder. The activation value is passed through an Above or Below node to get a trigger event when the value goes from high to low. The Go Below output trigger was renamed to Clicked to be more descriptive, made external, and connected to the reset trigger.

View Comments (0)