Input week

individual assignment:

  •       measure something: add a sensor to a microcontroller board
  •       that you have designed and read 

Here  is a link to the pcb->(link to electronic design week)


group assignment:

  •       probe an input device's analog levels and digital signals
group assignment


So for this week I WAS going to start by working on step response but I decided to try different stuff

I began by milling Adrian´s Step Response board. I decided to start by following his example because I felt that unfortunatly this week I did not have a lot of time since i was participating in THE NORTH ATLANTIC YOUTH BLUE INNOVATION CAMP.
I had to remill the board a few times, the first 2 times I had the iron WAY TOO HOT and burnt off the traces.
For the third one the FR1 wasn't properly fastened to the machine so the milling failed (whoops)


BUTTON

I had a button on my Xiao board so that´s the first input I used.
At first when I began using the button it worked in reverse, it had a high value until I pressed so instead of working as an on button it worked as an off button. 
I learned to have it start with a low value :)
The global instructor Lambert Rulindana, pointed out that its better not to drive the vibrating motor directly from the microcontroller pins since the Back EMF can destroy the controller. He reccomended I use a transistor instead (BJT,MOSFETS,IGBJT,…) as switch controlled by the microcontroller.

button1vibration2

Here is a video of the code active:



Button code

generated with Chat GPT : link

// Define pin numbers
const int buttonPin = D7; // Pin for the button
const int ledPin = D6; // Pin for the LED
const int motorPin = D10; // Pin for the vibration motor


// Variable to store the button state
int buttonState = 0;


void setup() {
// Initialize the button pin as an input with internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);


// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);


// Initialize the vibration motor pin as an output
pinMode(motorPin, OUTPUT);


// Start with the LED and motor turned off
digitalWrite(ledPin, LOW);
digitalWrite(motorPin, LOW);
}


void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);


// Check if the button is pressed
if (buttonState == HIGH) {
// Turn on the LED
digitalWrite(ledPin, HIGH);


// Turn on the vibration motor
digitalWrite(motorPin, HIGH);
} else {
// Turn off the LED
digitalWrite(ledPin, LOW);


// Turn off the vibration motor
digitalWrite(motorPin, LOW);
}
}

Explanation to code:

I used GitHub Copilot to explain the code.

GitHub Copilot: The active selection of code consists of two function implementations: `setup()` and `loop()`. Let's break down each function and understand what it does.

The `setup()` function is a special function in Arduino programming that is called once when the microcontroller starts up. Its purpose is to initialize the pins and set their initial states. In this code, the `setup()` function performs the following tasks:

1. It sets the `buttonPin` as an input pin with an internal pull-up resistor. This means that the pin is configured to read the state of a button connected to it. The internal pull-up resistor ensures that the pin is pulled to a high voltage level when the button is not pressed, preventing it from floating and producing unreliable readings.

2. It sets the `ledPin` as an output pin. This pin is connected to an LED, and by setting it as an output, we can control the LED by turning it on or off.

3. It sets the `motorPin` as an output pin. This pin is connected to a vibration motor, and by setting it as an output, we can control the motor by turning it on or off.

4. It sets the initial states of the LED and motor pins to `LOW`, which means they are initially turned off. This ensures that when the microcontroller starts up, the LED and motor are in a known state.

Moving on to the `loop()` function, this is another special function in Arduino programming that is called repeatedly after the `setup()` function. Its purpose is to perform the main tasks of the program. In this code, the `loop()` function performs the following tasks:

1. It reads the state of the button by using the `digitalRead()` function on the `buttonPin`. The `digitalRead()` function returns the state of the pin, which can be either `HIGH` or `LOW`. In this case, a `HIGH` state indicates that the button is pressed, while a `LOW` state indicates that the button is not pressed.

2. It checks if the button is pressed by using an `if` statement. If the `buttonState` is `HIGH`, it means the button is pressed, and the code inside the `if` block is executed.

3. Inside the `if` block, it turns on the LED by setting the `ledPin` to `HIGH` using the `digitalWrite()` function. This will illuminate the LED.

4. It also turns on the vibration motor by setting the `motorPin` to `HIGH` using the `digitalWrite()` function. This will activate the motor and make it vibrate.

5. If the button is not pressed (i.e., the `buttonState` is `LOW`), the code inside the `else` block is executed.

6. Inside the `else` block, it turns off the LED by setting the `ledPin` to `LOW` using the `digitalWrite()` function. This will turn off the LED.

7. It also turns off the vibration motor by setting the `motorPin` to `LOW` using the `digitalWrite()` function. This will stop the motor from vibrating.

The `loop()` function then repeats these steps continuously, checking the state of the button and controlling the LED and motor accordingly. This allows the program to respond to the button press in real-time.

Overall, this code sets up the pins, initializes their states, and continuously checks the state of a button to control the LED and vibration motor.


Accelerometer

I wanted to use an accelerometer
First I began gathering videos on youtube into a playlist


 
 This code lets the board show the value of the X Y Z through serial communication

Here is a video example :




Accelerometer code

generated with Chat GPT

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>

// Create an ADXL345 sensor instance
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void setup() {
Serial.begin(9600);
Serial.println("ADXL345 Accelerometer Test");

// Initialize the sensor
if (!accel.begin()) {
Serial.println("No ADXL345 detected ... Check your wiring!");
while (1);
}

// Set the range to whatever is appropriate for your project
accel.setRange(ADXL345_RANGE_16_G);

// Display the sensor details
sensor_t sensor;
accel.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" m/s^2");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" m/s^2");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" m/s^2");
Serial.println("------------------------------------");
Serial.println("");

// Set the range to whatever is appropriate for your project
accel.setRange(ADXL345_RANGE_16_G);

// Display the range settings
Serial.print ("Range: +/- ");
switch(accel.getRange()) {
case ADXL345_RANGE_16_G: Serial.print("16 "); break;
case ADXL345_RANGE_8_G: Serial.print("8 "); break;
case ADXL345_RANGE_4_G: Serial.print("4 "); break;
case ADXL345_RANGE_2_G: Serial.print("2 "); break;
}
Serial.println(" g");
Serial.println("");
delay(500);
}

void loop() {
// Get a new sensor event
sensors_event_t event;
accel.getEvent(&event);

// Display the results (acceleration is measured in m/s^2)
Serial.print("X: ");
Serial.print(event.acceleration.x); Serial.print(" ");
Serial.print("Y: ");
Serial.print(event.acceleration.y); Serial.print(" ");
Serial.print("Z: ");
Serial.print(event.acceleration.z); Serial.print(" ");
Serial.println("");
//Serial.println("m/s^2 ");

// Delay before the next reading
delay(500);
}



Explanation to code :

I used GitHub Copilot to explain the code.

GitHub Copilot: This code is an example of using the ADXL345 accelerometer sensor with an Arduino board. Let's break down the code into two parts: the `setup()` function and the `loop()` function.

The `setup()` function is called once when the Arduino board is powered on or reset. Its purpose is to initialize the necessary settings and perform any necessary setup tasks. Here's what the `setup()` function does:

1. It starts the serial communication with a baud rate of 9600. This allows the Arduino to communicate with the computer via the USB cable.
2. It prints a message to the serial monitor saying "ADXL345 Accelerometer Test".
3. It checks if the ADXL345 sensor is detected. If the sensor is not detected, it prints a message saying "No ADXL345 detected ... Check your wiring!" and enters an infinite loop, effectively stopping the program execution.
4. It sets the range of the accelerometer to 16g (gravity). The range determines the maximum acceleration value that the sensor can measure.
5. It retrieves the sensor details such as the sensor name, driver version, unique ID, maximum value, minimum value, and resolution. These details are printed to the serial monitor for informational purposes.
6. It sets the range of the accelerometer to 16g again. This is redundant and could be removed.
7. It displays the range settings of the accelerometer (16g, 8g, 4g, or 2g) by using a switch statement. The selected range is printed to the serial monitor.
8. It adds a delay of 500 milliseconds before proceeding to the `loop()` function.

The `loop()` function is called repeatedly after the `setup()` function. It contains the main logic of the program. Here's what the `loop()` function does:

1. It declares a variable `event` of type `sensors_event_t` to store the sensor event data.
2. It calls the `getEvent()` function of the accelerometer object to retrieve a new sensor event and stores it in the `event` variable.
3. It prints the X, Y, and Z acceleration values from the sensor event to the serial monitor.
4. It adds a delay of 500 milliseconds before the next reading.

This code continuously reads the acceleration values from the ADXL345 sensor and prints them to the serial monitor. The delay between readings ensures that the readings are not taken too frequently.

Files

pcb

MF Xiao PCB
MF Xiao Pro
MF traces svg
MF interior svg

codes
Button
Accelerometer Code