Skip to content

9. Input devices

This week assignment

  • Group assignment:

    • Probe an input device(s)’s analog levels and digital signals
    • Document your work on the group work page and reflect on your individual page what you learned
    • Individual assignment:

    • Measure something: add a sensor to a microcontroller board that you have designed and read it.

Group page input week

Progress

  • [ ] Grupp assignment - [ ]
    • [ ] Probe an input device(s)´s analog levels and signals.
    • [ ] Document your work on the group work page and reflect n your induvidual page what you learned.
    • [ ] Document
  • [ ] Induvidual assignment
    • [ ] Measure something: add a sensor to a microcontroller board that you have deigned and read it.
    • [ ] Document that process
    • [ ]

Research

This week i did some research on movment sensors that might work and be used in my final project both PIR and ultrasonic sensors, after i did go throug what we have in the lab and can be used in my final assignment i decided to use the Adafruit PIR motin sensor his spec fits great to my final project
Here are it s main spec:

  • Size: Rectangular
  • Price: $10.00 at the Adafruit shop
  • Output: Digital pulse high (3V) when triggered (motion detected) digital low when idle (no motion detected). Pulse lengths are determined by resistors and capacitors on the PCB and differ from sensor to sensor.
  • Sensitivity range: up to 20 feet (6 meters) 110° x 70° detection range
  • Power supply: 5V-12V input voltage for most modules (they have a 3.3V regulator), but 5V is ideal in case the regulator has different specs

What i learn from the group project

I am an electrician and a teacher so i did not learn much of this activity cause i already know this thing. But in the case of probing the signal from the sensor i can clearly see that there is no signal comming from the signal pin unless there is a movement in front of the sensor, i can also se that the voltage inn to the sensor is always around 5V(if feed with 5V) as long as the circut is powerd on. Measusirng current i can also se that current is only flowing throug the circut if it is voltage and resistor in the circut, that said we dont have current unless we have voltage U=IXR Were u - Voltage I Ö current and R egeals resistance of this formula we can clearly se that if one element of this thre are missing we do not have voltage or current in the circut. But one of this elements will always be in every circut that is residance we will always have some resisdance in the circut it self like in the coppar or other material in the circut.

Making the induvidual assignment

I made circut with the adafruit PIR motin sensor this week i had also in the PCB one resistor and LED i want the circut to work like that if there was any motion in front of the sensor it will turn on the LED of no motin in front of the sensor it will turn off the led.

BOM

Material Quantity
Koppar plate 1
RP-2040 Xiao 1
Con header socket 1x7 2
Con header socket 1x3 1
pin 1x7 p2,54mm 2
resistor 1K 1
LED any color 1
Adafruit Pir motion sensor 1
Solder tin Little

I used Kicad to make the PCB board and cut it out in the Roland MDX-20 mill in the lab in previus assignment i have gone over the process of how that is done so im not covering that here now.

Then o solder the board programed it and testet.

Here are few picture:

new_project This picture shows how the board was milled in the first attempt, it seams that me and the Roland mill was not friend at that time.

new_project
Here is my secondt attempt on milling that board and this time it went great.

new_project

Here is how the board look when it was ready.
design file kicad

gerber files

COde

// C++ code
//
#include <Arduino.h>
#include <U8g2lib.h>


U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);

int motion = 0;

void setup()
{
  pinMode(27, INPUT);
  pinMode(28, OUTPUT);
}

void loop()
{
  if (digitalRead(27) == 1) {
    digitalWrite(28, HIGH);
  } else {
    digitalWrite(28, LOW);
  }
  delay(10); // Delay a little bit to improve simulation performance
}

And this was the outcome:

import machine
import utime
from ssd1306 import SSD1306_I2C

# Define pin numbers
motion_sensor_pin = machine.Pin(27, machine.Pin.IN)
led_pin = machine.Pin(28, machine.Pin.OUT)

# Initialize I2C for OLED display
i2c = machine.I2C(0, scl=machine.Pin(21), sda=machine.Pin(20))
oled = SSD1306_I2C(128, 64, i2c)

def display_message(message):
    oled.fill(0)
    oled.text(message, 0, 24)
    oled.show()

while True:
    # Read the state of the motion sensor
    motion_state = motion_sensor_pin.value()

    # Check if motion is detected
    if motion_state == 1:
        # Turn on the LED
        led_pin.value(1)
        print("Motion detected! LED is ON.")
        # Display message on OLED
        display_message("Motion detected!")
    else:
        # Turn off the LED
        led_pin.value(0)
        print("No motion. LED is OFF.")
        # Display message on OLED
        display_message("No motion.")

    # Small delay to avoid bouncing
    utime.sleep(0.1)

This was not the outcome i expected Copilot added alot to the code that was not realy needed in it, even added hardvare like oled screen that was not supposed to be in this project, so i had to clean up the code. Here is the final version:

``` import machine import utime

Define pin numbers

motion_sensor_pin = machine.Pin(27, machine.Pin.IN) led_pin = machine.Pin(28, machine.Pin.OUT)

while True: # Read the state of the motion sensor motion_state = motion_sensor_pin.value()

# Check if motion is detected
if motion_state == 1:
    # Turn on the LED
    led_pin.value(1)
    print("Motion detected! LED is ON.")
  er
else:
    # Turn off the LED
    led_pin.value(0)
    print("No motion. LED is OFF.")


# Small delay to avoid bouncing
utime.sleep(0.1)
```

As you can see this code is very simple but this is very simple project to do the reason i started with C++ code is that i am little bit familiar with C++ and can make simple program with C++ my self. as this is very simple code it was not hard for me to do. I am not very familiar with python so i decided to get help from copilot to convert te code, i was very surprised by how much Copilot change it some good some i didint like. Lika adding hardware to this code(that OLED Screen) was not requerd and should not have been added to the code.
One of the thing i like about Copilots change is that it starts by define the pin numbers, in a small program like this one its not realy needed to define the pin numbers, but in a longer and more complex code were you using the same pins again and agian it can be realy good to dfine the pin numbers in case u ever need to change the pin number then you only have to do that in one place not all around the program code.

ANd in the end small video of how it worked: