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.
We 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);
}
That’s all for now!