더 재밌게 놀기 연구소
※ Ultrasonic Sensor (external library)
Circuit
Add zip library to include it in the source code.
=> This NewPing library converts ping time to distance and print result.
Source Code
//www.elegoo.com
//2016.06.13
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(500); // Wait 500ms between pings (about 2 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
}
==> Result
※ Membrane switch (Keypad)
Circuit
Download and add KeyPad Library
http://playground.arduino.cc/Code/Keypad
Source Code
//www.elegoo.com
//2016.06.13
/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey){
Serial.println(customKey);
}
}
==> Result : If you press any button then the value of the button will be display
※ Temperature and Humidity Sensor
Circuit
add SimpleDHT library
Source Code
//www.elegoo.com
//2016.06.13
#include <SimpleDHT.h>
// for DHT11,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dht11;
void setup() {
Serial.begin(9600);
}
void loop() {
// start working...
Serial.println("=================================");
Serial.println("Sample DHT11...");
// read with raw sample data.
byte temperature = 0;
byte humidity = 0;
byte data[40] = {0};
if (dht11.read(pinDHT11, &temperature, &humidity, data)) {
Serial.print("Read DHT11 failed");
return;
}
Serial.print("Sample RAW Bits: ");
for (int i = 0; i < 40; i++) {
Serial.print((int)data[i]);
if (i > 0 && ((i + 1) % 4) == 0) {
Serial.print(' ');
}
}
Serial.println("");
Serial.print("Sample OK: ");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.println(" %");
// DHT11 sampling rate is 1HZ.
delay(1000);
}
==> Result
'IoT > Arduino' 카테고리의 다른 글
MIT App Inventor - Android App - Mega Millions Jackpot number generator (0) | 2017.01.30 |
---|---|
Updated Source Code - Automated Mung Bean Sprouts Growing Machine (0) | 2017.01.27 |
Arduino Project 1 - Automated Mung Bean Sprouts Growing Machine (0) | 2017.01.23 |
[Arduino] Motion Sensor and Water Level Sensor (0) | 2017.01.17 |
[Arduino] Joystick, LED Dot Matrix and ADXL335 Module (0) | 2017.01.17 |
[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 |