Application of Programming: Scratch & Arduino Dr. Hong Sun Hag Professor Seoil University
Open Source Platform?
An assortment of common input & output components
Common components like helping us build functional electronic circuits
A sampling of wires & accessories for power and circuitry
BASICS OF ELECTRICITY AND CIRCUITS As the name suggests, an electronic circuit is circular. Electrons flow from the positive end of the power source (for example, a battery) around the circuit to the negative end of the same power source. The easiest way to understand the physics of what is happening inside an electric circuit is to compare it to a water tank system. Water in a pipe flows just like electrons in a wire. These electrons are what form the electric current that powers the components of the circuit.
Ex.1 LED Blink You will need: 1 × Arduino UNO 1 × solderless breadboard 1 × standard LED 1 × 220 Ohm resistor 2 × jumper cables
To start the circuit, connect a jumper wire from pin 10 on the Arduino To start the circuit, connect a jumper wire from pin 10 on the Arduino. This is the point at which the Arduino starts talking to the circuit. You can use any numbered pin from the right side of the Arduino — just make sure your code refers to the correct one. To make sure the ideal amount of current flows through the LED, the resistor is needed. Unlike the LED, it doesn’t matter which way it is inserted into the circuit. Whether pin 10 is allowing current through or not (controlled by your code) will determine whether the LED is on or off. Another jumper wire then connects to the negative side of the LED and returns to ground to complete the circuit. Simple! Once completed, your circuit should look something like the image below. Plug this into your computer via USB. The next task is to set up the Arduino to work with software coding.
LED_Blink.ino /* Blink - Turns on an LED on for one second, then off for one second, repeatedly. Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN takes care of use the correct LED pin whatever is the board used. If you want to know what pin the on-board LED is connected to on your Arduino model, check the Technical Specs of your board at https://www.arduino.cc/en/Main/Products This example code is in the public domain. modified 8 May 2014 by Scott Fitzgerald modified 2 Sep 2016 by Arturo Guadalupi */ // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(10, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(10, LOW); // turn the LED off by making the voltage LOW
Tools -> Board Tools -> Serial Port Sketch -> Verify/Compile
Bluetooth Paring Test We need 4 wires to connect Vcc (+5V) GND TX Rx
// Arduino code for basic Bluetooth test. << BT_test // Arduino code for basic Bluetooth test. << BT_test.ino >> #include <SoftwareSerial.h> SoftwareSerial BTSerial(2, 3); //Connect HC-06. Use your (TX, RX) settings void setup() { Serial.begin(9600); Serial.println("Hello!"); BTSerial.begin(9600); // set the data rate for the BT port } void loop() // BT –> Data –> Serial if (BTSerial.available()) { Serial.println(BTSerial.read()); // Serial –> Data –> BT if (Serial.available()) { BTSerial.write(Serial.read());
At Google Play, Install app -> BlueTerm When finished the installation of BlueTerm, Android phone may connect with HC-06 of Arduino by pairing Pin number : 1234 Device number : *****(e.g. smu001 .. )
Pairing When pairing completes, You may click the serial monitor window like below figure.
Ex.2 LED Control You will need: 1 × Arduino UNO 1 * Bluetooth HC-06 1 × solderless breadboard 1 × standard LED 1 × 220 Ohm resistor 2 × jumper cables
<< LED_BT.ino >> #include <SoftwareSerial.h> SoftwareSerial BTSerial(2,3); byte a=0; int LED=8; void setup(){ Serial.begin(9600); Serial.println("Hello!"); BTSerial.begin(9600); pinMode(LED,OUTPUT); } void loop(){ if(BTSerial.available()){ a = BTSerial.read(); BTSerial.write(a); if(a==49){ BTSerial.write("led on "); digitalWrite(LED,HIGH); if(a==48){ BTSerial.write("led off "); digitalWrite(LED,LOW); << LED_BT.ino >>
Ex.3 Digital Switch You will need: 1 × Arduino UNO 1 × solderless breadboard 1 × standard switch 1 × 10K Ohm resistor 2 × jumper cables
<< Switch_BT.ino >> #include <SoftwareSerial.h> SoftwareSerial BTSerial(2,3); int button = 13; // Pin setup void setup(){ Serial.begin(9600); Serial.println("Hello!"); BTSerial.begin(9600); pinMode(button,INPUT); } void loop(){ if(digitalRead(button) == LOW) { BTSerial.write("0"); if(digitalRead(button) == HIGH) BTSerial.write("1"); << Switch_BT.ino >>
Ex.4-1 LDR Serial Monitoring
// These constants won't change. << LDR_Serial.ino >> const int analogInPin = A1; // Analog input pin that the potentiometer is attached to const int analogOutPin = 9; // Analog output pin that the LED is attached to int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out) void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); } void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: analogWrite(analogOutPin, outputValue); // print the results to the serial monitor: Serial.print("sensor = "); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue); // wait 2 milliseconds before the next loop, for the analog-to-digital converter to settle // after the last reading: delay(20);
Ex.4-2 LDR B/T Monitoring You will need: 1 × Arduino UNO 1 * Bluetooth HC-06 1 × breadboard 1 × LDR 1 × 4.7K Ohm resistor 2 × jumper cables
// << LDR_BT.ino >> #include <SoftwareSerial.h> SoftwareSerial BTSerial(2, 3); //Connect HC-06. (TX, RX) settings void setup() { Serial.begin(9600); Serial.println("Hello!"); BTSerial.begin(9600); // set the data rate for the BT port } void loop() int light = analogRead(A5); BTSerial.println(light); delay(200);
Ex.5 Ultra Sonic Monitoring
// << Sonic_Serial.ino >> #define ECHOPIN 2 // echo pulse receive pin #define TRIGPIN 3 // trigger signal sending pin void setup(){ Serial.begin(9600); pinMode(ECHOPIN, INPUT); pinMode(TRIGPIN, OUTPUT); } void loop(){ digitalWrite(TRIGPIN, LOW); // trigger pin 2[uS] “LOW” delayMicroseconds(2); digitalWrite(TRIGPIN, HIGH); // “ranging” triggering 10[uS] “HIGH” delayMicroseconds(10); digitalWrite(TRIGPIN, LOW); // trigger pin “LOW” int distance = pulseIn(ECHOPIN, HIGH); // pulse time reading distance= distance/58; // calculate distance via pulse time Serial.println(distance); delay(50); // waiting 50[mS] for next action
Ex.6 Motor B/T Controlling
#include <SoftwareSerial.h> // << Motor_BT.ino >> SoftwareSerial BTSerial(8, 9); const int motorPins = 3; // motor connect D3 void setup() { BTSerial.begin(9600); } void loop() { if (BTSerial.available()) { char ch = BTSerial.read(); if(ch >= '0' && ch <= '9') // “ch”is number 0 ~9 int speed = map(ch, '0', '9', 0, 255); // Convert 0~9 to 0~255 analogWrite(3, speed); BTSerial.println(" "); BTSerial.println(speed); else { BTSerial.print("Unexpected character "); BTSerial.println(ch); } }
Q/A & Have a Good Time!!
Dr. Hong Sun Hag Professor at Seoil Univesity hongsh@seoil.ac.kr