PWM (Pulse Width Modulation)
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.
※ Tri LED control
Circuit
Source Code
#define RED 11
#define GREEN 10
#define BLUE 9
void setup() {
// put your setup code here, to run once:
randomSeed(analogRead(0));
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(RED, random(255));
analogWrite(GREEN, random(255));
analogWrite(BLUE, random(255));
delay(1000);
}
※ Control Tri LED using Buttons
Source Code
#define RED 11
#define GREEN 10
#define BLUE 9
#define RED_BUTTON 4
#define GREEN_BUTTON 3
#define BLUE_BUTTON 2
int r=0, g=0, b=0;
void setup() {
// put your setup code here, to run once:
pinMode(RED_BUTTON, INPUT);
pinMode(GREEN_BUTTON, INPUT);
pinMode(BLUE_BUTTON, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(RED_BUTTON) == HIGH) {
++r;
if(r>255) {
r=0;
}
}
if(digitalRead(GREEN_BUTTON) == HIGH) {
++g;
if(g>255) {
g=0;
}
}
if(digitalRead(BLUE_BUTTON) == HIGH) {
++b;
if(b>255) {
b=0;
}
}
analogWrite(RED,r);
analogWrite(GREEN,g);
analogWrite(BLUE,b);
delay(10);
}
Youtube Tutorial of above examples (in Korean) : https://www.youtube.com/watch?v=7h8TJtRekNI&list=PL0Vl139pNHbe-JlsydLg3NFRk6nC_cC7w&index=4
※ Passive Buzzer (piezo speaker)
Source Code
To include pitches.h, first copy the file to Sketch.
(Examples - 02. Digital - toneMelody)
#include "pitches.h"
int melody[] = {
NOTE_G4,
NOTE_G4,
NOTE_A5,
NOTE_A5,
NOTE_G4,
NOTE_G4,
NOTE_E4,
NOTE_G4,
NOTE_G4,
NOTE_E4,
NOTE_E4,
NOTE_D4,
0,
NOTE_G4,
NOTE_G4,
NOTE_A5,
NOTE_A5,
NOTE_G4,
NOTE_G4,
NOTE_E4,
NOTE_G4,
NOTE_E4,
NOTE_D4,
NOTE_E4,
NOTE_C4,
0 };
int noteDurations[] = {
1,1,1,1,
1,1,2,
1,1,1,1,
3,1,
1,1,1,1,
1,1,2,
1,1,1,1,
3,1
};
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
for(int thisNote = 0; thisNote < 26; thisNote++) {
int noteDuration = 250 * noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
}
==> Buzzer (Piezo speaker) will play the Korean children song.
※ Let's make a Piano using the Buzzer
Circuit
Source Code
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
int pins[] = {2,3,4};
int notes[] = {NOTE_E4, NOTE_D4, NOTE_C4};
void setup() {
// put your setup code here, to run once:
for(int i=0; i<3; i++) {
pinMode(pins[i], INPUT);
}
pinMode(8, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
for(int i=0; i<3; i++) {
if(digitalRead(pins[i] == HIGH)) {
tone(8, notes[i], 20);
}
}
}
Youtube Tutorial of above examples (in Korean) : https://www.youtube.com/watch?v=irbyDUGmNYk&index=5&list=PL0Vl139pNHbe-JlsydLg3NFRk6nC_cC7w
'IoT > Arduino' 카테고리의 다른 글
[Arduino] Motion Sensor and Water Level Sensor (0) | 2017.01.17 |
---|---|
[Arduino] Joystick, LED Dot Matrix and ADXL335 Module (0) | 2017.01.17 |
[Arduino] Ultrasonic, Keypad and Temperature and Humidity Sensor (0) | 2017.01.15 |
[Arduino] 7 Segment, Servo, LCD and Thermometer (0) | 2017.01.14 |
[Arduino] Using 74HC595 8-bit serial-in/serial or parallel-out shift register (0) | 2017.01.12 |
[Arduino] Library and RGB LED (0) | 2017.01.11 |
[Arduino] Servo Motor (0) | 2017.01.10 |
[Arduino] LCD and 7 Segment (0) | 2017.01.10 |
[Arduino] Illuminance (Photocell) and Ultrasonic Sensor (0) | 2017.01.09 |
[Arduino] Arduino Basic and control button and LEDs (0) | 2017.01.08 |