This week describes my understanding of how to use Input Devices. It also includes how to generate an analog output (PWM) using pin 9, how to use an oscilloscope and multimeter for analysis, and how to integrate different sesnor with the microcontroller.
For More About group AssignmentWe generate an analog output (PWM) using pin 9. A LED is connected to see the signal effect, and the oscilloscope and multimeter are used for analysis.
void setup() {
pinMode(9, OUTPUT);
}
void loop() {
for (int u = 0; u < 255; u++){
analogWrite(9, u);
delay(50);
}
}
The Joystick Module consists of two potentiometers, each controlling an axis (X and Y).
Connect the joystick to Arduino: A0 and A1 for analog readings, pin 2 for the button.
int x_Pin = A0;
int y_Pin = A1;
int button_Pin = 2;
void setup(){
Serial.begin(9600);
pinMode(button_Pin, INPUT_PULLUP);
}
void loop(){
Serial.print("X: "); Serial.print(analogRead(x_Pin));
erial.print("\tY: "); Serial.print(analogRead(y_Pin));
Serial.print("\tButton: "); Serial.println(digitalRead(button_Pin));
delay(100);
}
Axis X | Axis Y | |
---|---|---|
Up | 1023 | 527 |
Down | 0 | 527 |
Right | 512 | 1023 |
Left | 512 | 0 |
Center | 512 | 527 |
The LM35 is an analog temperature sensor with an output range of 0V to 1.5V.
float temp = 0.0;
int temp_Pin = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
temp = analogRead(temp_Pin);
Serial.print("temperature: ");
Serial.println(temp);
delay(100);
}
float temp_C = (5.0 * temp * 100.0) / 1024.0;
The HC-SR04 sensor calculates distance using ultrasonic waves.
const int Trig = 2;
const int Echo = 3;
float tim = 0.0;
float dist = 0.0;
void setup(){
Serial.begin(9600);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
digitalWrite(Trig, LOW);
}
void loop(){
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
tim = pulseIn(Echo, HIGH);
dist = tim / 59.0;
Serial.print("Distance: ");
Serial.print(dist);
Serial.println(" cm");
delay(100);
}
For this week we have to do a board attached with Microcontroller and then work on the
As we have worked on the previous weeks for PCB Design and Electronics Production. we are gonna repeat the same process for the Board production
Initially we're setting up the process for the Design as we're gonna design the product for Joystick Input, SDA, SCL ports and then Servomotor ports
Initially designed up the Kicad Schematic for the Board that we are gonna design
Now we gonna change that into PCB design using PCB Editor
After Designing the Board exported it as nc file from the MoDS CE and imported it to Wegster CNC for milling and afterwards get that final Product is
The Problem is due to the form of greenish layer on the Coppper Board and the other one is well cleaned and secured with mineral oil after fabrication
Intially I have soldered try to turn board it doesn't show any output and then I came to know the solder trace for the GND wasn't done at best
Now we get the solder and make up the Board and next we are gonna add the joystick to our microcontroller and afterwards showing the Graph out in Serial Plotter in arduino
#define VRX_PIN 2 // A0
#define VRY_PIN 3 // A1
#define SW_PIN 0 // Digital button
void setup() {
Serial.begin(115200);
pinMode(SW_PIN, INPUT_PULLUP); // Joystick button is active LOW
}
void loop() {
int xValue = analogRead(VRX_PIN);
int yValue = analogRead(VRY_PIN);
int swState = digitalRead(SW_PIN);
// Format for Serial Plotter: label:value, label:value
Serial.print("X:");
Serial.print(xValue);
Serial.print(",Y:");
Serial.print(yValue);
Serial.print(",SW:");
Serial.println(swState == LOW ? 0 : 4095); // show pressed/released as 0 or 4095
delay(50); // smoother graph
}
#define VRX_PIN 2
: Assigns pin 2 to read X-axis analog values.#define VRY_PIN 3
: Assigns pin 3 to read Y-axis analog values.#define SW_PIN 0
: Assigns pin 0 to read joystick button state.Serial.begin(115200);
: Begins serial communication for plotting or debugging.pinMode(SW_PIN, INPUT_PULLUP);
: Sets the button pin as input with an internal pull-up resistor.int xValue = analogRead(VRX_PIN);
: Reads and stores X-axis value.int yValue = analogRead(VRY_PIN);
: Reads and stores Y-axis value.int swState = digitalRead(SW_PIN);
: Reads the joystick button state (pressed/released).Serial.print("X:")
: Labels the X value for plotting.Serial.print(xValue)
: Prints the X-axis value.Serial.print(",Y:")
: Adds Y-axis label.Serial.print(yValue)
: Prints Y-axis value.Serial.print(",SW:")
: Adds switch label.Serial.println(swState == LOW ? 0 : 4095);
: Converts button state to 0 or 4095 for Serial Plotter graph scaling.delay(50);
: Adds a 50ms delay for smoother plotting without excessive data refresh.
That’s all for now!