Encoder example

The encoder sensor device has to be connected to the sensor module. Encoder sensor device has a two 3,5 mm plugs what has to be connected to the sensor module 3,5 mm input jackets. The controller and sensor modules must be connected.

The jackets must be inserted to the right input. Four channel input to D jacket (right) and three channel input to the A jacket(left).

Needed libraries

lib_deps = ITTIoT, ClickEncoder

The example code will print out and send to the broker encoder’s current position value. The value increases when rotating in one direction and decreases when rotating in another direction. Additionally, the encoder wheel has a switch, and if it is pushed, then the message will be sent to the serial port.

#include <Arduino.h>
#include <ittiot.h>
#include <Ticker.h>
#include <ClickEncoder.h>
#define WIFI_NAME "name"
#define WIFI_PASSWORD "password"
// Defining pins as need for the encoder
#define ENC_PINA 12
#define ENC_PINB 13
#define ENC_BTN   0
ClickEncoder encoder = ClickEncoder(ENC_PINA, ENC_PINB, ENC_BTN, ENC_STEPS_PER_NOTCH);
Ticker encTicker;
bool encFlag;
uint16_t button;
// Function started after the connection to the server is established.
void iot_connected()
  Serial.println("MQTT connected callback");
  iot.log("IoT encoder example!");
void setEncFlag()
void setup()
  Serial.begin(115200); // setting up serial connection parameter
  //iot.setConfig("wname", WIFI_NAME);
  //iot.setConfig("wpass", WIFI_PASSWORD);
  iot.printConfig(); // print json config to serial
  iot.setup(); // Initialize IoT library
  // Activating additional button functions
  encTicker.attach(1, setEncFlag);
//Main code, which runs in loop
void loop()
  iot.handle(); // IoT behind the plan work, it should be periodically called
  delay(10); // wait for 0.01 second
  static uint32_t lastService = 0;
  // Sending message “Button”, when the analogue value is bigger less then 100
  if(analogRead(A0) < 100)
    Serial.println( "button");
  if (lastService + 1000 < micros())
    lastService = micros();
  static int16_t last, value;
  value += encoder.getValue();
  // Send encoder reading to serial monitor, if it has changed
  if(value != last)
    last = value;
    Serial.print("Encoder Value: ");
  // Publishing encoder value in MQTT broker
      encFlag = false;
      String msg = String(value);
      iot.publishMsg("enc", msg.c_str());
en/iot/examples/encoder.txt · Last modified: 2021/03/05 02:03 by heiko.pikner
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0