Skip to content

13. Input devices

This week I worked on adding a sensor to a microcontroller board that you have designed and read it. [Reference]

Group assignment

This week of group assignment, We try to probe an input device’s analog and digital signals.. Click HERE to see more detail of the group assignment.


Individual assignment

Heroshot

IR sensor

I plan to make a board that can sensor something passthrough. Because my project, the Ball-In game machine, is designed for users to get points when the props(ping-pong balls) go through the ring, and the ring is controlled by the users. And I assume that there will be a sensor or a trigger to count the ball.
So I search for what sensor I could use, and I found this, IR distance sensor.

ir_distance_sensor.jpg

And it’s one of the ways to detect something passthrough. So I choose this sensor for the test and my assignment this week.

PCB design

I’ve used my ESP32 board which is the board for final project.

Then I’ve used mods to turn it into Gcode.
And I’ve used EdytorNC to send the Gcode to the machine.

Connecting wires

IR sensor have three wires: GND(Ground), VCC(5V Power), SIG(Signal).

Programming

After making sure there are no short-circuit and wires connect mistakenly. I’ve programmed it with the code below, so the I2C LCD will display the status of the IR sensor and the Count of detections.

If the IR sensor detects nothing, reset IR_status, and shows the score and status of the sensor in the LCD.
If the IR sensor detects anything, the score increase by 1, and shows the score and status of the sensor in the LCD.
If the IR sensor keeps detecting nothing, shows the score and status of the sensor on the LCD.

Note: IR_status is a value used to prevent the score keep increasing in a detection.

#include <LiquidCrystal_I2C.h>

#define IRSensor (14)

LiquidCrystal_I2C lcd(0x26,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

int Score = 0;

int IR_staus = 0;

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

  pinMode(IRSensor, INPUT); // sensor pin INPUT

  lcd.init();                      // initialize the lcd
  lcd.backlight();
  lcd.print(">   --------   <");
  lcd.setCursor(0,1);
  lcd.print("Hello, world!");

  lcd.setCursor(0,1);
  lcd.print("IR: ");
  lcd.setCursor(6,1);
  lcd.print("Count: ");
}

void loop() {

  int statusSensor = digitalRead(IRSensor);

  if (statusSensor == 1 && IR_staus == 1) {
    IR_staus = 0;    
    lcd.setCursor(4,1);
    lcd.print(statusSensor);
    lcd.setCursor(13,1);
    lcd.print(Score);
  } else if (statusSensor == 0 && IR_staus == 0) {
    Score = Score + 1;
    IR_staus = 1; 
    lcd.setCursor(4,1);
    lcd.print(statusSensor);
    lcd.setCursor(13,1);
    lcd.print(Score);
  } else {
    lcd.setCursor(4,1);
    lcd.print(statusSensor);
    lcd.setCursor(13,1);
    lcd.print(Score);
  }

}

Result

And here is the result:



Last update: June 28, 2022