Maker.io main logo

Arduino Ultrasonic Range Finder, OLED, Rotary Encoder and Saving Setting

2026-04-20 | By Ron Cutts

License: GNU Lesser General Public License Displays EEPROM Arduino ESP32

In this tutorial, we will use an Arduino MEGA, OLED LCD, Ultrasonic range finder module, rotary encoder, and Visuino to SET and display the ultrasonic range on the LCD and set the limit distance with a red LED. We will be able to save the selected range by clicking on a rotary encoder button. Watch a demonstration video.

What You Will Need


The Arduino Mega 2560

Ultrasonic Range Finder

Rotary Encoder

OLED LCD

Red LED

Breadboard

Jumper wires

Visuino program: Download Visuino

What You Will Need

What You Will Need photo 2

What You Will Need photo 3

What You Will Need photo 4

What You Will Need photo 5

What You Will Need photo 6

What You Will Need photo 7

The Circuit

  • Connect GND from Arduino to breadboard pin (gnd)

  • Connect 5V pin from Arduino to breadboard pin (positive)

  • Connect SCL from Arduino to OLED LCD pin (SCL)

  • Connect SDA from Arduino to OLED LCD pin (SDA)

  • Connect OLED LCD pin (VCC) to breadboard pin (positive)

  • Connect OLED LCD pin (GND) to breadboard pin (GND)

  • Connect Ultrasonic module pin (VCC) to breadboard pin (positive)

  • Connect Ultrasonic module pin (GND) to breadboard pin (GND)

  • Connect Ultrasonic module pin (ECHO) to Arduino pin digital (3)

  • Connect Ultrasonic module pin (TRIG) to Arduino pin digital (2)

  • Connect digital pin (13) from Arduino to LED pin (positive)

  • Connect LED pin (negative) to breadboard pin (GND)

  • Connect rotary encoder pin[+] to breadboard pin (positive)

  • Connect rotary encoder pin[GND] to breadboard pin (GND)

  • Connect rotary encoder pin[CLK] to Arduino digital pin (6)

  • Connect rotary encoder pin[DT] to Arduino digital pin (7)

  • Connect rotary encoder pin[SW] to Arduino digital pin (8)

The Circuit

Start Visuino, and Select the Arduino MEGA Board Type

To start programming the Arduino, you will need to have the Arduino IDE installed from here: http://www.arduino.cc/.

Please be aware that there are some critical bugs in Arduino IDE 1.6.6. Make sure that you install 1.6.7 or higher; otherwise, this Instructable will not work! If you have not done so, follow the steps to set up the Arduino IDE to program ESP8266! The Visuino also needs to be installed. Start Visuino as shown in the first picture. Click on the "Tools" button on the Arduino component (Picture 1) in Visuino. When the dialog appears, select "Arduino Mega" as shown in Picture 2

Start Visuino, and Select the Arduino MEGA Board Type

Start Visuino, and Select the Arduino MEGA Board Type photo 2

In Visuino, add components.

  • Add "Start" component

  • Add "MultiSource" component

  • Add "Integer To Analog" component

  • Add "Rotary Encoder Sensor" component

  • Add "Integer To Analog" component

  • Add "Ultrasonic Ranger" component

  • Add "Compare Range" component

  1. Under the Properties window, select "Max", click on a Pin icon, and select "Float SinkPin"

  • Add "Display OLED" I2C component

  1. Double-click on an "OLED" component, and on the right side select "Text Field", drag it to the left, and under properties set size:2 , x:40 , y:40

  2. Select another "Text Field", drag it to the left and under properties set size:2 , x:40 , y:0

  3. Select "Draw Text", drag it to the left and under properties set size:2 ,Text: S-

  4. Select another "Draw Text", drag it to the left and under properties set size:2 ,y:40, Text: D-

  5. Select another "Draw Text", drag it to the left, and under properties set size:2 ,x:10, y:20, Text: Saved

  • Add "Button" component

  • Add "Inverter" component

  • Add "Up Down Counter" component

  1. Double click on "Up Down Counter" component and select "set value" on the right, now drag it to the left side

In Visuino Add Components

In Visuino Add Components photo 2

In Visuino Add Components photo 3

In Visuino Add Components photo 4

In Visuino Add Components photo 5

In Visuino Add Components photo 6

In Visuino Add Components photo 7

In Visuino Add Components photo 8

In Visuino Add Components photo 9

In Visuino: ​Connecting Components

  • Connect Arduino Digital pin[8] to Button1 pin [in]

  • Connect Button1 pin[out] to Inverter1 pin [in]

  • Connect Nutton1 pin [Out] to DisplayOled1>Elements draw text3> pin[clock]

  • Connect Arduino Digital pin[7] to RotaryEncoderSensor1 pin [Direction]

  • Connect Arduino Digital pin[6] to RotaryEncoderSensor1 pin [Clock]

  • Connect Arduino Digital pin[3] to UltraSonicRanger pin [Echo]

  • Connect Arduino Serial[0] pin [Out] to DisplayOLED1 pin [In]

  • Connect Arduino Modules Eeprom Elements Integer1 pin[Out123] to UpDownCounter2 pin [123 Value]

  • Connect inverter1 pin[Out] to Arduino Modules Eeprom Elements Integer1 pin [Remember]

  • Connect Start1 pin [Out] to MultiSource1 pin [in]

  • Connect MultiSource1 pin [0] to Arduino Modules Eeprom Elements Integer1 pin [Recall]

  • Connect MultiSource1 pin [1] to UpDownCounter2 pin[in]

  • RotaryEncoderSensor1 pin [up] to UpDownCounter2 pin[up]

  • RotaryEncoderSensor1 pin [down] to UpDownCounter2 pin[down]

  • Connect UpDownCounter2 pin [123 Out] to IntegerToAnalog1 pin [132 In] and OLED1 elements TextField2 pin [132 In]

  • Connect IntegerToAnalog1 pin[out] to CompareRange1 pin[max]

  • Connect UltrasonicRanger1 pin[out] to CompareRange1 pin[in] and DisplayOLED1 text Field1 pin[In]

  • Connect CompareRange1 pin [out] to Arduino Digital pin[13]

  • Connect UltrasonicRanger1 pin [Trigger] DisplayOLED1 pin[refresh] and Arduino digital pin[2]

  • Connect UpDownCounter2 pin [Out132] DisplayOLED1 Elements Text Field2 pin[132 In] and Arduino Serial[0] pin[132in ] and to Arduino Modules Eeprom Elements Integer1 pin [132 In]

  • Connect DisplayOLED1 pin [Out] to Arduino I2C pin [In]

In Visuino: ​Connecting Components

Generate, Compile, and Upload the Arduino Code

In Visuino, at the bottom, click on the "Build" Tab, make sure the correct port is selected, then click on the "Compile/Build and Upload" button.

Generate, Compile, and Upload the Arduino Code

Play

If you power the Arduino Mega module, the OLED LCD will start showing the value number for the distance of any obstacle. If you put any obstacle near the ultrasonic module, the value will change, and the LED will flash.
You can select the range of detection by rotating the encoder; the value should change on the LCD. If you wish to save this value, just press down the rotary encoder, and the value will be saved and loaded the next time you turn on the Arduino.

Congratulations! You have completed your Stepper motor project with Visuino. Also attached is the Visuino project that I created for this. You can download and open it in Visuino: https://www.visuino.eu

Downloads

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.