Learn electronics, coding, and projects — step by step.

012 - MakeCode MicroBit: Brickcell Rotary Encoder

George Bantique October 11, 2023 No Comments

Table of Contents

A rotary encoder is an electromechanical device used to convert the angular position or rotation of an object into an electrical signal. It's commonly used in various applications, such as measuring the position of knobs on electronic devices, controlling the movement of motors and machinery, or even in computer mice. Rotary encoders can be of two types: absolute and incremental.

The primary difference between absolute and incremental rotary encoders lies in how they represent and communicate position or rotation information:

1. Absolute Rotary Encoder

  • Absolute encoders provide a unique digital code for each possible position in a full 360-degree rotation.
  • They can directly indicate the exact position without the need for any additional reference point.
  • When power is applied or when queried, they immediately report the absolute position.
  • They are often used in applications where knowing the exact position is critical, such as in CNC machines or robotic arms.

2. Incremental Rotary Encoder

  • Incremental encoders generate pulse signals as the shaft rotates, and these pulses are counted to determine the relative position and direction of rotation.
  • They do not provide information about the absolute position without a reference point (a starting or “home” position).
  • Incremental encoders are typically used where relative movement or changes in position are more important than absolute position—such as speed and distance measurement.

In summary: Absolute encoders give the exact position at any moment, while incremental encoders give information about relative changes in position and direction. The choice between the two depends on the specific requirements of your application.

Hardware Instruction

Let's explore how to connect the Rotary Encoder module to a micro:bit microcontroller and utilize MakeCode for programming.

  1. Connect the Rotary Encoder GND pin to micro:bit GND pin.
  2. Connect the Rotary Encoder VCC pin to micro:bit 5V pin.
  3. Connect the Rotary Encoder SA pin to micro:bit pin 0.
  4. Connect the Rotary Encoder SB pin to micro:bit pin 1.
  5. Connect the Rotary Encoder SW pin to micro:bit pin 2.

Software Instruction

Now, let's start programming the micro:bit to utilize the Rotary Encoder module.

  1. Login to https://makecode.microbit.org/ using your Microsoft account.
  2. Create a new project by clicking the "New Project" button. Suggested name: "rotary-encoder-test".
  3. Click the "Extensions" block just under the "Math" block.
  4. Type https://github.com/gbantique/brickcell-rotary-encoder/ in the search bar.
  5. Select the "brickcell-rotary-encoder" from the search results. The "rotary encoder" block should appear under the "Brickcell" block.
  6. Copy the code provided below.

Brickcell.onPress(function () {
  count = 0;
  serial.writeLine("" + count);
});
Brickcell.onRotate(RotationDirection.Right, function () {
  count += 1;
  serial.writeLine("" + count);
});
Brickcell.onRotate(RotationDirection.Left, function () {
  count += -1;
  serial.writeLine("" + count);
});
let count = 0;
Brickcell.initRotary(DigitalPin.P0, DigitalPin.P1, DigitalPin.P2);
serial.setBaudRate(BaudRate.BaudRate115200);
count = 0;
serial.writeLine("" + count);
  1. Open a Serial Monitor such as the Termite terminal app. Set the baud rate to 115200 bps.

Or you can make a copy of my created project in your MakeCode workspace:

https://makecode.microbit.org/S42524-23812-11415-25786/

Expected Result

If you carefully follow the provided instructions above, you should be able to view a counter value with the following characteristics:

  • Increments by 1 every time the rotary encoder is rotated to the right (clockwise).
  • Decrements by 1 every time the rotary encoder is rotated to the left (counterclockwise).
  • Resets to 0 every time the rotary main shaft is pressed.
×

Leave a Reply

Required fields are marked *






Related Articles: (by Categories)