Cat Detector!
Learn how to use an OLED display and an Ultrasonic sensor with this funny and easy to build Cat Detector!
Components and supplies
1
Ultrasonic Sensor - HC-SR04 (Generic)
1
Arduino UNO
1
Graphic OLED, 128 x 64
Project description
Code
Code
c_cpp
1#include <Arduino.h> 2#include <U8g2lib.h> 3#ifdef U8X8_HAVE_HW_SPI 4#include <SPI.h> 5#endif 6#ifdef U8X8_HAVE_HW_I2C 7#include <Wire.h> 8#endif 9U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display 10 11// Define pins for ultrasonic sensor 12int const trigPin = 10; 13int const echoPin = 9; 14 15void setup(void) { 16 u8g2.begin(); // Oled display begins 17 pinMode(trigPin, OUTPUT); // trig pin will have pulses output 18 pinMode(echoPin, INPUT); // echo pin should be input to get pulse width 19} 20 21void loop(void) { 22 int duration, distance; // Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters 23 24 // Output pulse with 1ms width on trigPin 25 digitalWrite(trigPin, HIGH); 26 delay(1); 27 digitalWrite(trigPin, LOW); 28 29 duration = pulseIn(echoPin, HIGH); // Measure the pulse input in echo pin 30 31 distance = (duration/2) / 29.1; // Distance is half the duration devided by 29.1 (from datasheet) 32 33 // if distance less than 0.5 meter and more than 0 (0 or less means over range) 34 if (distance <= 50 && distance >= 0) { 35 u8g2.clearBuffer(); // clear the internal memory 36 u8g2.setFont(u8g2_font_inr30_mf); // choose a suitable font 37 u8g2.drawStr(0,50,"CAT"); // write something to the internal memory 38 u8g2.sendBuffer(); // transfer internal memory to the display 39 } else { 40 u8g2.clearBuffer(); // clear the internal memory 41 u8g2.sendBuffer(); // transfer internal memory to the display 42 } 43 44 delay(30); 45}
Code
c_cpp
1#include <Arduino.h> 2#include <U8g2lib.h> 3#ifdef U8X8_HAVE_HW_SPI 4 5#include <SPI.h> 6#endif 7#ifdef U8X8_HAVE_HW_I2C 8#include <Wire.h> 9 10#endif 11U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, 12 /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the 13 Display 14 15// Define pins for ultrasonic sensor 16int const trigPin = 10; 17int 18 const echoPin = 9; 19 20void setup(void) { 21 u8g2.begin(); // 22 Oled display begins 23 pinMode(trigPin, OUTPUT); // trig pin will have pulses 24 output 25 pinMode(echoPin, INPUT); // echo pin should be input to get pulse width 26} 27 28 29void loop(void) { 30 int duration, distance; // Duration will be the 31 input pulse width and distance will be the distance to the obstacle in centimeters 32 33 34 // Output pulse with 1ms width on trigPin 35 digitalWrite(trigPin, HIGH); 36 37 delay(1); 38 digitalWrite(trigPin, LOW); 39 40 duration = pulseIn(echoPin, 41 HIGH); // Measure the pulse input in echo pin 42 43 distance = (duration/2) 44 / 29.1; // Distance is half the duration devided by 29.1 (from datasheet) 45 46 47 // if distance less than 0.5 meter and more than 0 (0 or less means over 48 range) 49 if (distance <= 50 && distance >= 0) { 50 u8g2.clearBuffer(); 51 // clear the internal memory 52 u8g2.setFont(u8g2_font_inr30_mf); 53 // choose a suitable font 54 u8g2.drawStr(0,50,"CAT"); // 55 write something to the internal memory 56 u8g2.sendBuffer(); // 57 transfer internal memory to the display 58 } else { 59 u8g2.clearBuffer(); 60 // clear the internal memory 61 u8g2.sendBuffer(); // 62 transfer internal memory to the display 63 } 64 65 delay(30); 66}
Downloadable files
U8g2 library
You will need to instal de U8g2 library to control the display
U8g2 library
Protoboard
Protoboard
Protoboard
Protoboard
Comments
Only logged in users can leave comments