Skip to main content Link Search Menu Expand Document (external link)

Output Devices – ToF-based Range Meter

Table of contents
  1. Output Devices – ToF-based Range Meter
    1. Group Assignment for Output Devices
    2. Design
    3. Fabrication – Milling and Soldering
    4. Programming
      1. Initial Test of the OLED Display
      2. Result of the ToF-based Range Meter
      3. Extension – ToF-based Range Meter and Morse Code Decoder
      4. Code Snippets

Group Assignment for Output Devices

measure the power consumption of an output device.

shown in Week 9 of EECS Group Assignments.

Design

Following the idea of the ToF-based range sensor (VL53L0X) in Week 8, I decided to use the VL53L0X ToF sensor to measure the distance to the object, and display the distance on the OLED screen (PEMENOL 0.96-inch; 128×64 pixel resolution; Compatible with Adafruit SSD1306 Library). The OLED screen (connected via rainbow wires) is connected to the second I²C channel of the ESP32, and the ToF sensor (on board) is connected to the first I²C channel.

image Refer to Week 8 for the detailed design of the PCB (using D21E18A1 as the main chip).

Fabrication – Milling and Soldering

Since the pins of VL53L0X sensor are all underneath the chip, I have to use the flow technique i.e., hot air to solder it. And it’s super satisfying to see the soldering process using hot air.

I also used the solder paste instead of solder for the rest of the soldering process. The main reason is that so long as the amount of solder paste is enough, the solder paste will be melted by the iron, and the solder paste will be evenly distributed on the PCB. And the solder paste is much easier to clean up than the solder.

image
Milled PCB
image
Soldered PCB

Programming

After bootloadering the D21E18, I am able to program it using Arduino IDE. One software side thing that Anthony reminded me of is using two channles of I²C, that is setting the Tools -> Serial Config -> ONE_UART_TWO_WIRE_ONE_SPI to use two I²C (wire) channels. And the second I²C channel (&Wire1) is connected to the OLED display, and the first I²C channel (&Wire) is connected to the ToF sensor.
image

Another hardware side bug is that at some point I occassionally cannot get any response from the sensor. And Anthony eventually helped me find out that the issue might be the jump resistor (0Ω) not soldered properly. And the observation is that the voltage after the jump resistor is not 3.3V, but 2.6V. And after I resoldered the jump resistor, the issue is gone.

Initial Test of the OLED Display

Firstly, I used the example of ssd1306_128x64_i2c.ino in Adafruit SSD1306 Library to test the OLED display.

I changed the I²C address to 0x3C according to the amazon comments and the I²C wire to &Wire1 for Line #35-36. And it works, as shown the video below!

1
2
#define SCREEN_ADDRESS 0x3C // 0x3C for 128x64 OLED https://www.amazon.com/PEMENOL-Display-0-96inch-Raspberry-Microcontroller/dp/B07F3KY8NF
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire1, OLED_RESET); // Wire1 for second I2C channel in D21E18A

Result of the ToF-based Range Meter

I further combined the ToF sensor and the OLED display to make a ToF-based range meter. The code is shown in Code Snippts. And the result is shown in the video below.

Extension – ToF-based Range Meter and Morse Code Decoder

As shown in the code below, I also merged the Morse Code Decoder in Week 6 to the ToF-based range meter. It works well standalone, but with the joint range meter, it did not work. The main issue might be that I did not use the interrpupt to read the button state and decode the morse code. The current display refresh follows the rate of the ToF sensor, which is way higher than that of entering Morse Code using the button.

Code Snippets

oled_tof_morse_code.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <SPI.h>
#include <Wire.h>
#include <VL53L0X.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     -1   // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // 0x3C for 128x64 OLED https://www.amazon.com/PEMENOL-Display-0-96inch-Raspberry-Microcontroller/dp/B07F3KY8NF
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire1, OLED_RESET); // Wire1 for second I2C channel in D21E18A

VL53L0X sensorX; // ToF sensor
const int pin_shutdown = 15; // Pin of ToF sensor SHUTDOWN

const int pin_led = 18; // Pin of the LED
const int pin_btn = 14; // Pin of the button

int ledState = HIGH;         
int buttonState = HIGH;             
int lastButtonState = HIGH;  

const long debounceDelay = 100; // debounce delay time in ms [originally 50 ms; increased to improve robustness to noise]
const long pauseInterval = 350;  // pause interval [ms]
long signalLength =   0;  // length of the signal
long pauseTime    =   0;  // pause time

const String dot  = "·";
const String dash = "—";
String morse = "";

bool newChar = false;
bool newLine = false;

void setup() {
  Serial.begin(9600);

  init_button_led();   // button and LED for Morse Code Decoder initialization
  init_tof_sensor();   // ToF sensor initialization
  init_oled_display(); // OLED display initialization  
}

void loop() {
  int range = read_tof_sensor();
  String code = read_morse_code();

  display_range_morse_code(range, code);
}

void init_button_led() { // button and LED for Morse Code Decoder initialization
  pinMode(pin_btn, INPUT_PULLUP); // use internal pull-up resistor [Neil & Anthony]
  pinMode(pin_led, OUTPUT);
}

void init_tof_sensor() { // ToF sensor initialization
  Wire.begin();

  pinMode(pin_shutdown, OUTPUT);
  digitalWrite(pin_shutdown, HIGH);

  sensorX.setTimeout(500);
  if (!sensorX.init())
  {
    Serial.println("Failed to detect and initialize sensorX!");
    while (1) {}
  }
}

void init_oled_display() { // OLED display initialization
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  // display.drawPixel(10, 10, SSD1306_WHITE);

  // testdrawchar();      // Draw characters of the default font
  // testdrawstyles();    // Draw 'stylized' characters  
}

int read_tof_sensor() {
  int rangeX = sensorX.readRangeSingleMillimeters();
  if (sensorX.timeoutOccurred()) { Serial.print(" SensorX TIMEOUT"); }
  // Serial.println(rangeX);
  return rangeX;
}

String read_morse_code() {
  String code = "";
  buttonState = digitalRead(pin_btn);
  
  if (!buttonState && !lastButtonState) { // button pressing status - gathering signal
    ++signalLength; 
  }
  else if(buttonState && !lastButtonState){ // button released - send signal (dot or dash)
    if (signalLength>debounceDelay && signalLength<2*pauseInterval) {
      morse = morse + dot;
    } 
    else if (signalLength>2*pauseInterval) {
      morse = morse + dash;
    }
    signalLength = 0; 
    digitalWrite(pin_led, LOW); 
  }
  else if (!buttonState && lastButtonState) { // button just pressed - reset signal
    pauseTime = 0;
    digitalWrite(pin_led, HIGH);  
    newChar = true;
    newLine = true;
  }
  else if (buttonState && lastButtonState) {  
    ++pauseTime;
    if (( pauseTime>4*pauseInterval ) && (newChar)) { 
      code = decoder(morse);
      newChar = false;
      morse = "";
    }
    if ((pauseTime>20*pauseInterval) && (newLine)) {
      code = "\n";
      newLine = false;
    }
  }
  lastButtonState = buttonState;
  delay(1); 

  Serial.print(code);
  return code; 
}

String decoder(String message)   {   
  String code = "";                        
  if (message == "·—")
    code = "A";
  else if (message == "—···")  
    code = "B";
  else if (message == "—·—·")  
    code = "C";
  else if (message == "—··")  
    code = "D";
  else if (message == "·")  
    code = "E";
  else if (message == "··—·")  
    code = "F";
  else if (message == "——·")  
    code = "G";
  else if (message == "····")  
    code = "H";
  else if (message == "··")  
    code = "I";
  else if (message == "·———")  
    code = "J";
  else if (message == "—·—")  
    code = "K";
  else if (message == "·—··")  
    code = "L";
  else if (message == "——")  
    code = "M";
  else if (message == "—·")  
    code = "N";
  else if (message == "———")  
    code = "O";
  else if (message == "·——·")  
    code = "P";
  else if (message == "——·—")  
    code = "Q";
  else if (message == "·—·")  
    code = "R";
  else if (message == "···")  
    code = "S";
  else if (message == "—")  
    code = "T";
  else if (message == "··—")  
    code = "U";
  else if (message == "···—")  
    code = "V";
  else if (message == "·——")  
    code = "W";
  else if (message == "—··—")  
    code = "X";
  else if (message == "—·——")  
    code = "Y";
  else if (message == "——··")  
    code = "Z";
  else if (message == "·————")  
    code = "1";
  else if (message == "··———")  
    code = "2";
  else if (message == "···——")  
    code = "3";
  else if (message == "····—")  
    code = "4";
  else if (message == "·····")  
    code = "5";
  else if (message == "—····")
    code = "6";
  else if (message == "——···")  
    code = "7";
  else if (message == "———··")  
    code = "8";
  else if (message == "————·")  
    code = "9";
  else if (message == "—————")  
    code = "0";
  else 
    code = message;
    
  message = ""; 
  Serial.print(code);
  return code;
}

void testdrawchar(void) {
  display.clearDisplay();

  display.setTextSize(1);      // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE); // Draw white text
  display.setCursor(0, 0);     // Start at top-left corner
  display.cp437(true);         // Use full 256 char 'Code Page 437' font

  // Not all the characters will fit on the display. This is normal.
  // Library will draw what it can and the rest will be clipped.
  for(int16_t i=0; i<256; i++) {
    if(i == '\n') display.write(' ');
    else          display.write(i);
  }

  display.display();
  delay(2000);
}

void testdrawstyles(void) {
  display.clearDisplay();

  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(F("Hello, world!"));

  display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  display.println(3.141592);

  display.setTextSize(2);             // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.print(F("0x")); display.println(0xDEADBEEF, HEX);

  display.display();
  delay(2000);
}

void display_range_morse_code(int range, String code) {
  display.clearDisplay();

  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.print(F("d = "));  
  display.print(range);
  display.print(F("mm"));  
  display.println(code);

  display.display();
  delay(10);  
}