File tree 5 files changed +60
-14
lines changed
1.Basics/ReadAnalogVoltage
5 files changed +60
-14
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
2
AnalogReadSerial
3
- Reads an analog input on pin 0, prints the result to the serial monitor
3
+ Reads an analog input on pin 0, prints the result to the serial monitor.
4
+ Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
4
5
5
6
This example code is in the public domain.
6
7
*/
7
8
9
+ // the setup routine runs once when you press reset:
8
10
void setup () {
11
+ // initialize serial communication at 9600 bits per second:
9
12
Serial.begin (9600 );
10
13
}
11
14
15
+ // the loop routine runs over and over again forever:
12
16
void loop () {
17
+ // read the input on analog pin 0:
13
18
int sensorValue = analogRead (A0);
19
+ // print out the value you read:
14
20
Serial.println (sensorValue);
15
21
delay (1 ); // delay in between reads for stability
16
22
}
Original file line number Diff line number Diff line change 4
4
5
5
This example code is in the public domain.
6
6
*/
7
+
8
+ // Pin 13 has an LED connected on most Arduino boards.
9
+ // give it a name:
10
+ int led = 13 ;
7
11
12
+ // the setup routine runs once when you press reset:
8
13
void setup () {
9
14
// initialize the digital pin as an output.
10
- // Pin 13 has an LED connected on most Arduino boards:
11
- pinMode (13 , OUTPUT);
15
+ pinMode (led, OUTPUT);
12
16
}
13
17
18
+ // the loop routine runs over and over again forever:
14
19
void loop () {
15
- digitalWrite (13 , HIGH); // set the LED on
16
- delay (1000 ); // wait for a second
17
- digitalWrite (13 , LOW); // set the LED off
18
- delay (1000 ); // wait for a second
20
+ digitalWrite (led , HIGH); // turn the LED on (HIGH is the voltage level)
21
+ delay (1000 ); // wait for a second
22
+ digitalWrite (led , LOW); // turn the LED off by making the voltage LOW
23
+ delay (1000 ); // wait for a second
19
24
}
Original file line number Diff line number Diff line change 5
5
This example code is in the public domain.
6
6
*/
7
7
8
+ // digital pin 2 has a pushbutton attached to it. Give it a name:
9
+ int pushButton = 2 ;
10
+
11
+ // the setup routine runs once when you press reset:
8
12
void setup () {
13
+ // initialize serial communication at 9600 bits per second:
9
14
Serial.begin (9600 );
10
- pinMode (2 , INPUT);
15
+ // make the pushbutton's pin an input:
16
+ pinMode (pushButton, INPUT);
11
17
}
12
18
19
+ // the loop routine runs over and over again forever:
13
20
void loop () {
14
- int sensorValue = digitalRead (2 );
15
- Serial.println (sensorValue);
16
- delay (1 ); // short delay in between reads for stability
21
+ // read the input pin:
22
+ int buttonState = digitalRead (pushButton);
23
+ // print out the state of the button:
24
+ Serial.println (buttonState);
17
25
}
18
26
19
27
Original file line number Diff line number Diff line change 5
5
using the analogWrite() function.
6
6
7
7
This example code is in the public domain.
8
-
9
8
*/
9
+
10
+ int led = 9 ; // the pin that the LED is attached to
10
11
int brightness = 0 ; // how bright the LED is
11
12
int fadeAmount = 5 ; // how many points to fade the LED by
12
13
14
+ // the setup routine runs once when you press reset:
13
15
void setup () {
14
16
// declare pin 9 to be an output:
15
- pinMode (9 , OUTPUT);
17
+ pinMode (led , OUTPUT);
16
18
}
17
19
20
+ // the loop routine runs over and over again forever:
18
21
void loop () {
19
22
// set the brightness of pin 9:
20
- analogWrite (9 , brightness);
23
+ analogWrite (led , brightness);
21
24
22
25
// change the brightness for next time through the loop:
23
26
brightness = brightness + fadeAmount;
@@ -29,3 +32,4 @@ void loop() {
29
32
// wait for 30 milliseconds to see the dimming effect
30
33
delay (30 );
31
34
}
35
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ ReadAnalogVoltage
3
+ Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
4
+ Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
5
+
6
+ This example code is in the public domain.
7
+ */
8
+
9
+ // the setup routine runs once when you press reset:
10
+ void setup () {
11
+ // initialize serial communication at 9600 bits per second:
12
+ Serial.begin (9600 );
13
+ }
14
+
15
+ // the loop routine runs over and over again forever:
16
+ void loop () {
17
+ // read the input on analog pin 0:
18
+ int sensorValue = analogRead (A0);
19
+ // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
20
+ float voltage = sensorValue * (5.0 / 1023.0 );
21
+ // print out the value you read:
22
+ Serial.println (voltage);
23
+ }
You can’t perform that action at this time.
0 commit comments