WEEK 8 : Input Devices

So based on my Final project idea I thought this week I would start testing some proximity sensors that would eventually go into the "moving coffee table" and connect it to a couple stepper motors. After last weeks recitation one of the Ta's suggested I started looking at the ultrasonic sensor HC-SR04 and at the VL53L1X time-of flight sensor and then on the architecture shop I found the PIR Motion Sensor so I thought I could try that one as well.

So I started with the VL53L1X time-of flight sensor by Pololu since it was the one recommended. I would say I started off with the right one since this One out of the three was the one that I feel performed better! I know spoiler alert. But it gave the most accurate readings of proximity and it was quite straight forward. also from previos weeks I recreated a board I found here by Adrian where he also runs a similar proximity sensor study.

Here is the code that I used and it can be found in the link above:


                    #include Wire.h
                        #include VL53L1X.h
                        
                        VL53L1X sensor;
                        
                        void setup() {
                          Serial.begin(9600);
                          Wire.begin();
                        
                          sensor.setTimeout(500);
                          if (!sensor.init()) {
                            Serial.println("Failed to detect and initialize sensor!");
                            while (1);
                          }
                        
                          // Start continuous measurements at a rate of one measurement every 50 ms.
                          sensor.startContinuous(50);
                        }
                        
                        void loop() {
                          Serial.print("Distance: ");
                          Serial.print(sensor.read());
                          if (sensor.timeoutOccurred()) {
                            Serial.print(" TIMEOUT");
                          }
                          Serial.println();
                        
                          delay(100);
                        }
                        

              
            

Then is when things started to get fun... I decided to get a Stepper motor and test how the proximity sensor would start behaving and if there was some lag with the response time. And as you can see below it was pretty accurate. I found the stepper motor was found in the architecture shop but its done by Pololu. Also the movement is a bit what im looking for in my running coffee table!

I then decided to test it on my little 3d printed mockup to see if the motion of the legs could be some what simulated at a small scale! I scaled up a bit with the stepper motor and while it works it looks more like its Salsa dancing than running away so definetley some work to be done!

Here is the full code to activate a stepper motor with the proximity sensor:


                    #include Wire.h        // Include the I2C library
                        #include VL53L1X.h     // Include the VL53L1X library
                        #include Servo.h       // Include the Servo library
                        
                        VL53L1X sensor;
                        Servo myservo;  // create servo object to control a servo
                        
                        void setup() {
                          Serial.begin(115200); // Start the serial communication with a baud rate of 115200
                          myservo.attach(26);   // attaches the servo on GIO26 to the servo object
                          
                          Wire.begin();         // Initialize the I2C
                          Wire.setClock(400000); // Use 400 kHz I2C
                          
                          sensor.setTimeout(500); // Set a timeout for sensor reading
                          if (!sensor.init()) {
                            Serial.println("Failed to detect and initialize sensor!");
                            while (1); // Infinite loop stops the program if sensor init failed
                          }
                          
                          sensor.setDistanceMode(VL53L1X::Long); // Use long distance mode
                          sensor.setMeasurementTimingBudget(50000); // Allow up to 50000 us (50 ms) for a measurement
                          
                          sensor.startContinuous(100); // Start continuous readings at a rate of one measurement every 50 ms.
                        }
                        
                        void loop() {
                          uint16_t distance = sensor.read(); // Read the distance measurement from the sensor
                          if (sensor.timeoutOccurred()) { 
                            Serial.print(" TIMEOUT"); // Check if a timeout occurred
                          } else {
                            Serial.print("Distance: ");
                            Serial.print(distance);
                            Serial.println(" mm");
                          }
                        
                          if (distance  100) {
                            // If the distance is less than 50, start moving the servo
                            for (int pos = 0; pos = 180; pos += 1) { // goes from 0 degrees to 180 degrees
                              myservo.write(pos); // tell servo to go to position in variable 'pos'
                              delay(1); // waits 15ms for the servo to reach the position
                            }
                            for (int pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
                              myservo.write(pos); // tell servo to go to position in variable 'pos'
                              delay(1); // waits 15ms for the servo to reach the position
                            }
                          }
                        }
                        

            

I then decided to test out the ultrasonic sensor. which I did not like a lot the distance measurements where not as accurate and it kept jumping a bit all over the place. I did connect it to the and OLED to visualize it there because using my desktop to visualize the serial monitor was not great. I also did not test the stepper motor on this guy since I felt the results would not be as good as with the VL53L1X.

Here is the full code that connects an ultrasonic sensor to an OLED to display the proximity. Also I made most of this codes by collaging existing codes on chat gpt


                            #include U8g2lib.h

                                // Initialize U8g2 library for your specific display type
                                // Replace 'U8G2_SSD1306_128X64_NONAME_F_HW_I2C' with the constructor for your display
                                U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
                                
                                // defines pin numbers
                                const int trigPin = D1;
                                const int echoPin = D0;
                                // defines variables
                                long duration;
                                int distance;
                                
                                void setup() {
                                  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
                                  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
                                  Serial.begin(9600); // Starts the serial communication
                                
                                  // Initialize the OLED display
                                  u8g2.begin();
                                
                                  // Set a larger font
                                  u8g2.setFont(u8g2_font_helvB14_tr); // Example of a larger font
                                }
                                
                                void loop() {
                                  // Clears the trigPin
                                  digitalWrite(trigPin, LOW);
                                  delayMicroseconds(2);
                                  // Sets the trigPin on HIGH state for 10 micro seconds
                                  digitalWrite(trigPin, HIGH);
                                  delayMicroseconds(10);
                                  digitalWrite(trigPin, LOW);
                                  // Reads the echoPin, returns the sound wave travel time in microseconds
                                  duration = pulseIn(echoPin, HIGH);
                                  // Calculating the distance
                                  distance = duration * 0.034 / 2;
                                  // Prints the distance on the Serial Monitor
                                  Serial.print("Distance: ");
                                  Serial.println(distance);
                                
                                  // Display the distance on the OLED
                                  u8g2.clearBuffer();          // clear the internal memory
                                  u8g2.setCursor(0, 20);       // set cursor position
                                  u8g2.print("Dist: ");
                                  u8g2.print(distance);
                                  u8g2.print(" cm");
                                  u8g2.sendBuffer();           // transfer internal memory to the display
                                }
                                

                    

I finally decided to test the PIR sensor which is basically a (Passive Infrared) sensor is a motion sensor that detects changes in infrared radiation to identify motion or presence of objects in its field of view. I used both the OLED to display when motion was detected and a blinking LED, I was just curious to see how accurate this was, and I have to say it was pretty sensitive it would sense every slight movement and then stop so probably not great for the project but still a pretty good test good link to read more about PIR if you are interested here

:D It kida walks!!

Full code to use the PIR with a stepper motor below:


                            #include U8g2lib.h

                                // Initialize U8g2 library for your specific display type
                                // Replace 'U8G2_SSD1306_128X64_NONAME_F_HW_I2C' with the constructor for your display
                                U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
                                
                                // defines pin numbers
                                const int trigPin = D1;
                                const int echoPin = D0;
                                // defines variables
                                long duration;
                                int distance;
                                
                                void setup() {
                                  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
                                  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
                                  Serial.begin(9600); // Starts the serial communication
                                
                                  // Initialize the OLED display
                                  u8g2.begin();
                                
                                  // Set a larger font
                                  u8g2.setFont(u8g2_font_helvB14_tr); // Example of a larger font
                                }
                                
                                void loop() {
                                  // Clears the trigPin
                                  digitalWrite(trigPin, LOW);
                                  delayMicroseconds(2);
                                  // Sets the trigPin on HIGH state for 10 micro seconds
                                  digitalWrite(trigPin, HIGH);
                                  delayMicroseconds(10);
                                  digitalWrite(trigPin, LOW);
                                  // Reads the echoPin, returns the sound wave travel time in microseconds
                                  duration = pulseIn(echoPin, HIGH);
                                  // Calculating the distance
                                  distance = duration * 0.034 / 2;
                                  // Prints the distance on the Serial Monitor
                                  Serial.print("Distance: ");
                                  Serial.println(distance);
                                
                                  // Display the distance on the OLED
                                  u8g2.clearBuffer();          // clear the internal memory
                                  u8g2.setCursor(0, 20);       // set cursor position
                                  u8g2.print("Dist: ");
                                  u8g2.print(distance);
                                  u8g2.print(" cm");
                                  u8g2.sendBuffer();           // transfer internal memory to the display
                                }