112-01 Introduction to Mechanical Engineering ARDUINO TUTORIAL MECHANICAL ENGINEERING CONCEPT 黃澤樺 , 賴昱堯 email: yuyaolai@ntu.edu.tw NATIONAL TAIWAN UNIVERSITY OUTLINES WHAT IS ARDUINO HOW TO START IDE: Coding with Arduino IDE Structure of a Arduino program Program examples How a breadboard works ULTRASONIC SENSOR --- HC-SR04 How it works Program and Signal visualization in Arduino IDE Stream data to MATLAB (optional) INTRO We can see Arduino as a Brain of a machine, which can easily transfer input (ex. signal from sensor) into output (ex. motor control). WHY Arduino An open-source hardware: anyone can manufacture Arduino boards Free, cross-platform IDE software: Arduino IDE The Arduino Programming Language is close to C, C++ Digital and analog input/output (I/O), serial communications interfaces: interact with other hardware For more information, please visit: https://www.arduino.cc/ Boards Ref: https://www.arduino.cc/en/hardware#boards-1 HOW TO START PINOUT Recommend 7V to 12V 5V Power Supply from USB port Digital Pins *14 (5V, 20mA) Analog Input *6 Ref: https://store.arduino.cc/products/arduino-uno-rev3 ARDUINO IDE To upload code to main board, an official software (Arduino IDE) is available. Where to download: www.arduino.cc -> Software Choose your system of laptop to download the right version. ---- Mac / Windows Feel free to donate or click "Just download". Ref: https://www.arduino.cc/en/software INTERFACE Select board Verify Upload Serial monitor Serial Plotter Sketchbook Boards manager Library manager Debug Search (Connected) STRUCTURE • There is no main() function which exists in C/C++ programs. • Instead, in Arduino code we MUST include setup() AND loop() functions. Blink.ino File > Exam p le s > 01. B as ics > B link Blink.ino --- Circuit Connection Arduino Programming Syntax (b as ically th e s am e as C/C+ +) myLEDblinks.ino Make an exte rn al LED blick myLEDblinks.ino --- Circuit Connection Make an exte rn al LED blick Make s u re to con n e ct a 330 Ω re s istor in s e rie s with an LED, or you will b low u p th e LED. Se e : Lig ht - Em ittin g Diod e s (LEDs ) | s p a rkf u n Mechanism of a Breadboard F ront B ack ULTRASONIC SENSOR HC-SR04 Concept By measuring the time between the emitting and receiving the ultrasonic, we can derive the distance between the sensor and object. Ref: https://www.handsontec.com/dataspecs/HC-SR04-Ultrasonic.pdf How it works j Set trigger pin to HIGH for 10µs to start the ranging. k The sensor will then transmits an ultrasonic burst of 8 pulses at 40 kHz, while the echo pin goes HIGH to initiate the echo signal. l If those pulses are reflected back, the echo pin goes low as soon as the signal is received, and we get the travel time (µs) of the pulses. m If those pulses are not reflected back, the echo signal times out and goes low after 38ms. Ref: https://lastminuteengineers.com/arduino-sr04-ultrasonic-sensor-tutorial/ Calculation 𝑡𝑖𝑚𝑒 𝜇𝑠 1 𝑠 𝑐𝑚 𝐷𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑐𝑚 = ∙ ∙ 34000 2 1000000 𝜇𝑠 𝑠 𝑖𝑛 𝐷𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑖𝑛 = 𝐷𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑐𝑚 ∙ 2.54( ) 𝑐𝑚 We assumed that the speed of sound is 340 (𝑚/𝑠) HC-SR04 --- Circuit Connection HC-SR04 int trig = 13; int echo = 12; int duration; double cm; double in; --- Code // Trig Pin number // Echo Pin number void setup() { Serial.begin(9600); // Serial Port begin pinMode(trig, OUTPUT); // set trig pin as output digitalWrite(trig, LOW); // set trig pin to LOW (no current) pinMode(echo, INPUT); // set echo pin as input } Ref: https://www.handsontec.com/dataspecs/HC-SR04-Ultrasonic.pdf Full code: https://www.space.ntu.edu.tw/navigate/a/#/s/5A8224C955AC435A99508D0B2609CB256BL HC-SR04 --- Code void loop() { digitalWrite(trig, HIGH); // set trig pin to HIGH (current flows through) delayMicroseconds(10); // wait for 10us (to start the ranging) digitalWrite(trig, LOW); // set trig pin to LOW (no current) duration = pulseIn(echo, HIGH); // start timeing when echo pin goes from LOW to HIGH // and stops timing when echo pin returns to LOW if (duration >= 38000) { Serial.print("Out range"); } else { cm = duration * 0.017; Serial.print("cm:"); Serial.print(cm); in = cm / 2.54; Serial.print(","); Serial.print("in:"); Serial.print(in); Serial.println(); } delay(100); // since the echo signal goes low after 38ms if those // pulses are not reflected back // plot label (must use a colon, no space after the // label) // plot variable // for plotting more than one variable // linebreak, update to value to plot // suggest to use over 60ms measurement cycle, in order to prevent trigger // signal to the echo signal } Ref: https://www.handsontec.com/dataspecs/HC-SR04-Ultrasonic.pdf Full code: https://www.space.ntu.edu.tw/navigate/a/#/s/5A8224C955AC435A99508D0B2609CB256BL HC-SR04 --- Serial Monitor & Serial Plotter Select “New Line” Select “9600 baud” HC-SR04 --- Stream data to MATLAB (optional) Change the If-Else part of the code. Now the output data is in the form: time,displacement if (duration >= 38000) { Serial.print("Out range"); } else { Serial.print(millis()/1000.0); Serial.print(","); Serial.print(cm); Serial.println(); // since the echo signal goes low after 38ms if those // pulses are not reflected back // print time (s) to serial // seperate displacement and time, used as delimiter // later in matlab // print displacement (cm) to serial // linebreak, which is actually two terminators "CR/LF" // (Carriage Return / Line Feed) } Full code: https://www.space.ntu.edu.tw/navigate/a/#/s/655CAA1471B548278D956D030A0E413D6BL HC-SR04 --- Stream data to MATLAB (optional) Make sure the Arduino IDE is now closed so that the COM port can be used by MATLAB. Check the COM port number (or find the COM port from Arduino IDE “Select board” beforehand) serialportlist("available") Assign the COM port number and baud rate. Then Configure the terminator in use (recall Serial.println() is equivalent to “CR/LF”) arduinoObj = serialport("COM4",9600); configureTerminator(arduinoObj,"CR/LF"); Full code: https://www.space.ntu.edu.tw/navigate/a/#/s/8165EC1F856F407DBDD781DA840577326BL HC-SR04 --- Stream data to MATLAB (optional) Read 100 data points from Arduino, and store it to an array for i = 1:100 % loop from 1 to n, step by increments of 1 data(i,1) = readline(arduinoObj); end Close the serial communication clear arduinoObj; Split the data into 2 arrays, which are time and displacement. Then convert the datatype of the arrays from “string” to “double” d = split(data, ","); ddouble = cellfun(@str2double, d); Full code: https://www.space.ntu.edu.tw/navigate/a/#/s/8165EC1F856F407DBDD781DA840577326BL HC-SR04 --- Stream data to MATLAB (optional) Plot the figure from the two arrays. figure % create a new figure, otherwise it will overwrite current figure plot(ddouble(:,1), ddouble(:,2),'-k','DisplayName','displacement y1'); % plot y1 over t, Solid line (-), black (k), use 'displacement y1' as Displayname % axis([0 15 -1.1 1.1]); % set the visible window grid on; % add grids xlabel('Time (s)','fontsize',12,'interpreter','latex') % set the x-label with name (with unit), fontsize and text interpreter (optional) ylabel('Displacement (m)','fontsize',12,'interpreter','latex') legend('location','northeast', 'fontsize',12,'interpreter','latex') % make the legend saveas(gcf, 'SHMfromArduino.svg') % save the current figure (gcf) to '.svg' Full code: https://www.space.ntu.edu.tw/navigate/a/#/s/8165EC1F856F407DBDD781DA840577326BL