selected input/output - sensors and actuators
TRANSCRIPT
![Page 1: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/1.jpg)
1 / 74
Sensors & Actuators
Selected I/OsEueung Mulyanahttps://eueung.github.io/012017/inoutCodeLabs | Attribution-ShareAlike CC BY-SA
![Page 2: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/2.jpg)
Outline
Preparation
Light/Laser Sensor
PIR Sensor
LM35 & DHT11
Processing & Local Visualization
Ultrasonic Sensor
2 / 74
![Page 3: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/3.jpg)
Preparation
3 / 74
![Page 4: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/4.jpg)
Preparation Notes
We reuse the previous nRF24L01+ example (Simple Remote Control) with some changes i.e.:for simplicity both nodes are now Nano-based, an LCD is attached to Node-2 for a better
communication and debugging experience.
In the next examples, the switch will be replaced with laser and PIRsensors.
4 / 74
![Page 5: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/5.jpg)
5 / 74
I2C LCD1602An LCD display that can display a max
of 16x2 characters.
As the pin resources of ucontroller is limited, your project maybe not able to use normal LCD shield after connected with acertain quantity of sensors/actuators i.e. if you are doing morethan a simple project, you may be out of pins using a normalLCD shield. With this I2C interface LCD module, you only need 2lines (I2C) to display information.
Interface: connect the I2C LCD1602 to the I2C port of Arduino(SDA - A4 and SCL - A5) and power this module with 5V.
Ref: arduino-info, elecrow
![Page 6: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/6.jpg)
Node-1 & Node-2 (Both with nRF24L01+)6 / 74
![Page 7: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/7.jpg)
Node-1 (Remote Switch / Sensor Node)7 / 74
![Page 8: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/8.jpg)
Node-2 (Display Node)8 / 74
![Page 9: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/9.jpg)
Node-19 / 74
![Page 10: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/10.jpg)
Node-210 / 74
![Page 11: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/11.jpg)
Install I2C LiquidCrystal Library11 / 74
![Page 12: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/12.jpg)
#include <SPI.h> #include "nRF24L01.h"#include "RF24.h"
RF24 myRadio (7, 8);
const int SW1 = 5;const int SW2 = 6;
byte addresses[][6] = {"1Node"}; int dataTransmitted;
int button1;int button2;
void setup() { pinMode(SW1, INPUT); pinMode(SW2, INPUT); button1 = 0; button2 = 1; dataTransmitted = 10;
Serial.begin(115200); delay(1000);
myRadio.begin(); myRadio.setRetries(0,15); myRadio.setChannel(108); myRadio.setPALevel(RF24_PA_MAX);
myRadio.openWritingPipe( addresses[0]); delay(1000);}
void loop() { int newButton = digitalRead(SW1); if (newButton != button1) { button1 = newButton;
if (button1 == HIGH){ dataTransmitted = 20; } 12 / 74
Node-1
![Page 13: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/13.jpg)
#include <SPI.h> #include "nRF24L01.h"#include "RF24.h"
RF24 myRadio (7, 8);
byte addresses[][6] = {"1Node"}; int dataReceived;
const int LED = 2;
#include <Wire.h>#include <LiquidCrystal_PCF8574.h>
LiquidCrystal_PCF8574 lcd(0x27);
int lcdexist;unsigned long started_waiting_at = 0;bool bklight = false;
void setup() { pinMode(LED, OUTPUT);
Serial.begin(115200); delay(1000);
myRadio.begin(); myRadio.setAutoAck(1); myRadio.setChannel(108); myRadio.setPALevel(RF24_PA_MAX);
myRadio.openReadingPipe(1, addresses[0]); myRadio.startListening();
Wire.begin(); Wire.beginTransmission(0x27); lcdexist = Wire.endTransmission();
lcd.begin(16, 2);}
void loop() { 13 / 74
Node-2
![Page 14: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/14.jpg)
Switch ON14 / 74
![Page 15: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/15.jpg)
Switch OFF15 / 74
![Page 16: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/16.jpg)
Light/Laser Sensor
16 / 74
![Page 17: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/17.jpg)
Laser Mini Transmitter & Receiver Module17 / 74
![Page 18: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/18.jpg)
Light/Laser Receiver ModuleUtilizing LDR and Voltage Comparator for Digital Output
18 / 74
![Page 19: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/19.jpg)
19 / 74
LDRLDR or Light Dependent Resistor (sometimes calleda photoresistor or photocell) is a two-terminal,resistive component that increases or decreases itsresistance depending on the light it senses. An LDRinitially has a very high resistance. But, as light fallson it, the resistance will drop, allowing more currentthrough.
Ref: Earthshine Design
![Page 20: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/20.jpg)
20 / 74
LDR Sensor ModuleLDR sensor module is used to detect the intensity oflight. It might have both analog output pin anddigital output pin labelled as AO and DOrespectively on the board. When there is light, theresistance of LDR will become low according to theintensity of light. The greater the intensity of light,the lower the resistance of LDR. The sensor has apotentiometer knob that can be adjusted to changethe sensitivity of LDR towards light.
Ref: LDR Sensor Module
![Page 21: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/21.jpg)
Switch Replacement with a Laser Receiver21 / 74
![Page 22: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/22.jpg)
Switch Replacement with a Laser Receiver22 / 74
![Page 23: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/23.jpg)
PIR Sensor
23 / 74
![Page 24: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/24.jpg)
24 / 74
PIR SensorA PIR (Passive Infra-Red) sensor is an electronicsensor that measures infrared light radiating fromobjects in its �eld of view. Normally this type ofsensor would be used as a motion or proximitysensor. Quite often they are referred to as: PIR,Motion, Proximity, or Pyroelectric sensor.
The sensor in the PIR detects or "reads" infrared radiation "emitted"from objects all around us. Each object with a temperature aboveabsolute zero will radiate infrared, even us humans, and even though wemere humans cannot see this. Note that the PIR just uses a relativelysimple sensor - it is most de�nitely not an IR camera!
Ref: Testing and Playing with PIR sensors @Tweaking4All
![Page 25: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/25.jpg)
Switch Replacement with a PIR Sensor25 / 74
![Page 26: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/26.jpg)
LM35 & DHT11
26 / 74
![Page 27: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/27.jpg)
LM35 & DHT1127 / 74
![Page 28: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/28.jpg)
28 / 74
LM35LM35 is a linear temperature sensor with the outputvoltage calibrated in centigrade celsius. 1 degreecelsius makes an output voltage of 10mV. So whenyou have room temperature at 22C the LM35 givesyou a voltage of 220mV = 0.22V.
The sensor has only 3 pins: VCC which can be between +4V and +20V,GND and Vout. Vout has to be connected to one of the analog inputs andthat's all - in most cases.
Ref: connecting an arduino with a LM35 temperature sensor @heliosoph
![Page 29: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/29.jpg)
29 / 74
DHT11DHT11 is a temperature and humidity sensor with acalibrated digital signal output.
The DHT11 sensor has four pins. Connect the �rst pin on left of sensor('VCC') to 5V of your board, the second pin ('Data') to a digital pin andthe fourth pin ('GND') to ground. When the connecting cable is shorterthan 20m, a 5K pull-up resistor between the second Pin of sensor and 5Vis recommended.
Ref: DHT11 Temperature and Humidity Sensor Example @arduino.org
![Page 30: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/30.jpg)
Install Library for DHTxx Sensors30 / 74
![Page 31: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/31.jpg)
float tlm35;
void setup() { Serial.begin(9600);
//analogReference(INTERNAL);}
void loop() { tlm35 = 0.0; for(int j = 0; j < 10; j++) { tlm35 += analogRead(A0); delay(20); } tlm35 = tlm35 * 5.0 * 10.0 / 1023.0;
//tlm35 = tlm35 * 1.1 * 10.0 / 1023.0;
Serial.println(tlm35);}
31 / 74
LM35
![Page 32: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/32.jpg)
#include "DHT.h"
#define DHTPIN 3 #define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() { Serial.begin(9600);
dht.begin();}
void loop() { delay(2000);
// Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); float t = dht.readTemperature();
if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; }
Serial.println(t);}
32 / 74
DHT11
![Page 33: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/33.jpg)
Local Visualization & UI
Processing
33 / 74
![Page 34: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/34.jpg)
34 / 74
ProcessingProcessing is a �exible software sketchbook and alanguage for learning how to code within thecontext of the visual arts.
Since 2001, Processing has promoted software literacy within the visualarts and visual literacy within technology. There are tens of thousands ofstudents, artists, designers, researchers, and hobbyists who useProcessing for learning and prototyping.
Ref: Processing.org
![Page 35: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/35.jpg)
35 / 74
Processing is an open source language/development tool for writing programs in othercomputers. Useful when you want those othercomputers to talk with an Arduino, for instance todisplay or save some data collected by the Arduino.
How to Work with Processing?Write Arduino sketch as usual, except for input/output intended tobe retrieved from or sent to ProcessingWrite Processing program that receives/sends data from/toArduino
If you just want to control an Arduino board from a Processing program,you may want to use the Arduino Library for Processing.
Ref: Arduino Playground - Processing
![Page 36: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/36.jpg)
Switch ExampleRef: Electronics @Processing.org 36 / 74
![Page 37: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/37.jpg)
int switchPin = 5;
void setup() { pinMode(switchPin, INPUT); Serial.begin(9600); delay(1000);}
void loop() { if (digitalRead(switchPin) == HIGH) { //Serial.println(1); Serial.write(1); } else { //Serial.println(0); Serial.write(0); } delay(100); }
37 / 74
Arduino
![Page 38: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/38.jpg)
import processing.serial.*;
Serial port; int val; String stext;
void setup() { size(200, 200); frameRate(10); port = new Serial(this, "/dev/ttyUSB0", 9600);}
void draw() { if (0 < port.available()) { val = port.read(); } background(255); if (val == 0) { fill(0); stext = "OFF"; } else { fill(255,0,0); stext = "ON"; } rect(50, 50, 100, 100);
textAlign(CENTER); textSize(30); fill(255); text(stext, 100, 112);}
38 / 74
Processing
![Page 39: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/39.jpg)
Using Arduino IDE Serial Plotter39 / 74
![Page 40: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/40.jpg)
Using Arduino IDE Serial Plotter40 / 74
![Page 41: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/41.jpg)
Controlling a ServoRef: Electronics @Processing.org 41 / 74
![Page 42: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/42.jpg)
Micro ServoTower Pro SG90 42 / 74
![Page 43: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/43.jpg)
#include <Servo.h>
Servo myservo; int servoPin = 4; int val = 0;
void setup() { myservo.attach(servoPin); Serial.begin(9600); }
void loop() { if (Serial.available()) { val = Serial.read(); } myservo.write(val); delay(15); }
43 / 74
Arduino
![Page 44: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/44.jpg)
import processing.serial.*;
Serial port; float mx = 0.0;
void setup() { size(200, 200); noStroke(); frameRate(10); port = new Serial(this, "/dev/ttyUSB0", 9600);}
void draw() { background(0); fill(204); rect(40, height/2-15, 120, 25);
float dif = mouseX - mx; if (abs(dif) > 1.0) { mx += dif/4.0; } mx = constrain(mx, 50, 149);
noStroke(); fill(255); rect(50, (height/2)-5, 100, 5); fill(204, 102, 0);
rect(mx-2, height/2-5, 4, 5); int angle = int(map(mx, 50, 149, 0, 180)); port.write(angle); }
44 / 74
Processing
![Page 45: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/45.jpg)
Arduino Built-In Example - Graph45 / 74
![Page 46: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/46.jpg)
void setup() { Serial.begin(9600);}
void loop() { Serial.println(analogRead(A0));
// wait a bit for the analog-to-digital converter // to stabilize after the last reading: delay(2);}
46 / 74
Arduino
![Page 47: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/47.jpg)
import processing.serial.*;
Serial myPort; // The serial portint xPos = 1; // horizontal position of the graphfloat inByte = 0;
void setup () { size(400, 300); myPort = new Serial(this, "/dev/ttyUSB0", 9600);
myPort.bufferUntil('\n'); background(0);}
void draw () { stroke(127, 34, 255); line(xPos, height, xPos, height - inByte);
if (xPos >= width) { xPos = 0; background(0); } else { xPos++; }}
void serialEvent (Serial myPort) { String inString = myPort.readStringUntil('\n');
if (inString != null) { inString = trim(inString); inByte = float(inString); println(inByte);
inByte = map(inByte, 0, 1023, 0, height); //inByte = map(inByte, 0, 100, 0, height); }}
47 / 74
Processing
![Page 48: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/48.jpg)
Circuit & Processing Plot48 / 74
![Page 49: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/49.jpg)
HC-SR04
Ultrasonic Sensor
49 / 74
![Page 50: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/50.jpg)
50 / 74
Ultrasonic SensorHC-SR04 ultrasonic ranging sensor provides 2cm to400cm of non-contact measurement functionalitywith a ranging accuracy that can reach up to 3mm. Itis a very a�ordable proximity/distance sensor thathas been used mainly for object avoidance invarious robotics projects.
Each HC-SR04 module includes an ultrasonic transmitter, a receiver anda control circuit. It has 4 pins: VCC, GND, Trigger and Echo pin.
![Page 51: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/51.jpg)
Install Arduino NewPing Library51 / 74
![Page 52: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/52.jpg)
Install Processing gra�ca Library52 / 74
![Page 53: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/53.jpg)
NewPing Example53 / 74
![Page 54: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/54.jpg)
#include <NewPing.h>
#define TRIGGER_PIN 3 #define ECHO_PIN 4 #define MAX_DISTANCE 300
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() { Serial.begin(9600); }
void loop() { delay(500); Serial.println(sonar.ping_cm()); }
54 / 74
Arduino
![Page 55: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/55.jpg)
import grafica.*;import processing.serial.*;
GPlot plot;
int i = 0; int points = 100; public float[] values;
Serial myPort; float inByte = 0;
void setup(){ size (900,450);
myPort = new Serial(this, "/dev/ttyUSB0", 9600);
GPointsArray points1 = new GPointsArray(points);
values = new float[points]; for (i = 0; i < points; i++) { points1.add(i,0); values[i]=0; }
plot = new GPlot(this); plot.setPos(25, 20); plot.setDim(750, 310);
plot.setXLim(0, points); plot.setYLim(0, 310);
plot.setTitleText("Distance"); plot.getXAxis().setAxisLabelText("Sample Time"); plot.getYAxis().setAxisLabelText("cm");
plot.setPoints(points1); }
void draw() { background(150);
GPointsArray points1 = new GPointsArray(points);
for (i = 0; i < values.length; i++) { 55 / 74
Processing
![Page 56: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/56.jpg)
Processing Plot with gra�ca Library56 / 74
![Page 57: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/57.jpg)
#include <NewPing.h>#include <Servo.h>
#define TRIGGER_PIN 3 #define ECHO_PIN 4 #define MAX_DISTANCE 300
Servo myservo; int servoPin = 5; int val = 0;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() { myservo.attach(servoPin); Serial.begin(9600); }
void loop() { val = analogRead(A0); val = map(val, 0, 1023, 0, 180); myservo.write(val); //delay(15);
delay(500); Serial.println(sonar.ping_cm()); }
Rotating SR04 with a Servo - Manually via a Potentiometer 57 / 74
Arduino
![Page 58: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/58.jpg)
#include <NewPing.h>#include <Servo.h>
#define TRIGGER_PIN 3 #define ECHO_PIN 4 #define MAX_DISTANCE 300
Servo myservo; int servoPin = 5; int val = 0; int lastval = 0; unsigned long timer;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() { myservo.attach(servoPin); Serial.begin(9600); timer = millis();}
void loop() { val = analogRead(A0); val = map(val, 0, 1023, 0, 180);
int dif = val - lastval;
if(abs(dif)>1) { val = lastval + dif/4; } myservo.write(val); lastval = val; delay(15);
if((millis()-timer)>500) { Serial.println(sonar.ping_cm()); timer = millis(); }}
Rotating SR04 with a Servo - Try this too! 58 / 74
Arduino
![Page 59: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/59.jpg)
HC-SR04 + SG9059 / 74
![Page 60: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/60.jpg)
SR04 SonarOur previous case can be rearranged to make a simple DIY Sonar
(SOund Navigation And Ranging) system.
The following example was modi�ed from Dejan Nedelkovski's code.
60 / 74
![Page 61: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/61.jpg)
61 / 74
![Page 62: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/62.jpg)
#include <NewPing.h>#include <Servo.h>
#define TRIGGER_PIN 3 #define ECHO_PIN 4 #define MAX_DISTANCE 300
Servo myservo; int servoPin = 5;
const int angle_start = 5; const int angle_end = 175; const int delaytime = 20;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() { myservo.attach(servoPin); Serial.begin(9600);}
void loop() { for(int i=angle_start;i<=angle_end;i++){ myservo.write(i); delay(delaytime);
Serial.print(i); Serial.print(","); Serial.print(sonar.ping_cm()); Serial.print("."); }
for(int i=angle_end;i>angle_start;i--){ myservo.write(i); delay(delaytime);
Serial.print(i); Serial.print(","); Serial.print(sonar.ping_cm()); Serial.print("."); }}
62 / 74
Arduino
![Page 63: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/63.jpg)
String distance="";String data="";String noObject;
float pixsDistance;int iAngle, iDistance;int index1=0;int index2=0;
final float MAX_DIST = 300; //-------------------------------------------------void setup() { size(1200, 700); smooth();
myPort = new Serial(this, "/dev/ttyUSB0", 9600); myPort.bufferUntil('.'); orcFont = loadFont("OCRAExtended-30.vlw");}//-------------------------------------------------void draw() { float covered_height = height*0.935; noStroke(); fill(0,4); rect(0, 0, width, covered_height);
fill(98,245,31); textFont(orcFont);
drawRadar(); drawLine(); drawObject(); drawText();}//-------------------------------------------------void serialEvent (Serial myPort) { data = myPort.readStringUntil('.');
data = data.substring(0,data.length()-1);
index1 = data.indexOf(","); angle= data.substring(0, index1); distance= data.substring(index1+1, data.length());
63 / 74
Processing
![Page 64: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/64.jpg)
Tools | Create Font64 / 74
![Page 65: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/65.jpg)
Tools | Create Font65 / 74
![Page 66: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/66.jpg)
66 / 74
drawRadar()
![Page 67: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/67.jpg)
67 / 74
drawText()
![Page 68: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/68.jpg)
68 / 74
drawRadar()drawText()
![Page 69: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/69.jpg)
69 / 74
drawLine()
![Page 70: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/70.jpg)
70 / 74
drawObject()
![Page 71: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/71.jpg)
71 / 74
![Page 72: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/72.jpg)
Refs/Resources
72 / 74
![Page 73: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/73.jpg)
Refs/Resources1. LDR Sensor Module Interface With Arduino2. Using a Photocell @Adafruit3. Photoresistor @ElectroSchematics4. Testing and Playing with PIR sensors @Tweaking4All5. Arduino Playground - Processing6. Processing Foundation7. Electronics - Processing.org8. Typography, Strings and Drawing Text9. Arduino library for DHT11DHT22, etc Temp & Humidity Sensors
10. A simple and con�gurable plotting library for Processing11. Clipart - Strain Gauge
73 / 74
![Page 74: selected input/output - sensors and actuators](https://reader034.vdocuments.us/reader034/viewer/2022051504/5a64d8ca7f8b9ac21c8b6d45/html5/thumbnails/74.jpg)
74 / 74
ENDEueung Mulyanahttps://eueung.github.io/012017/inoutCodeLabs | Attribution-ShareAlike CC BY-SA