This presentation includes introduction to inputs snd outputs on arduino UNO.
Size: 2.3 MB
Language: en
Added: Mar 09, 2025
Slides: 35 pages
Slide Content
Input/Output (Sensor and Actuators) Workshop on Arduino
Input/Output (Sensor and Actuators) Input and Output State Input Output Data Input Output Sensors Actuators
Input/Output
Input/Output
Input/Output State Input
Input/Output Example Code // Switch test program int ledPin = 12; // LED is connected to pin 12 int switchPin = 2; // Switch connected to digital pin 2 int val ; // variable for reading the pin status void setup () { pinMode ( ledPin , OUTPUT ); // Set the LED pin as output pinMode ( switchPin , INPUT ); // Set the switch pin as input } void loop (){ val = digitalRead ( switchPin ); // read input value and store it in val if ( val == HIGH ) { // check if the button is pressed digitalWrite ( ledPin , HIGH ); // turn LED on } if ( val == LOW ) { // check if the button is not pressed digitalWrite ( ledPin , LOW ); // turn LED off } }
State input Pull Down and Pull up
State input Example code /* * Switch and LED test program */ int ledPin = 12; // LED is connected to pin 12 int switchPin = 2; // switch is connected to pin 2 int val ; // variable for reading the pin status void setup () { pinMode ( ledPin , OUTPUT ); // Set the LED pin as output pinMode ( switchPin , INPUT ); // Set the switch pin as input } void loop (){ val = digitalRead ( switchPin ); // read input value and store it in val if ( val == LOW ) { // check if the button is pressed digitalWrite ( ledPin , HIGH ); // turn LED on } if ( val == HIGH ) { // check if the button is not pressed digitalWrite ( ledPin , LOW ); // turn LED off } }
Serial Library /* * Hello World! * * This is the Hello World! for Arduino. * It shows how to send data to the computer */ void setup () // run once, when the sketch starts { Serial.begin (9600); // set up Serial library at 9600 bps } void loop () // run over and over again { Serial.println ( "Hello world!" ); // prints hello with ending line break delay (1000); }
Sensors
Sensors
Actuators
Serial Communication (UART communication)
Serial Communication /* * Hello World! * * This is the Hello World! for Arduino. * It shows how to send data to the computer */ void setup () // run once, when the sketch starts { Serial.begin (9600); // set up Serial library at 9600 bps } void loop () // run over and over again { Serial.println ( "Hello world!" ); // prints hello with ending line break delay (1000); }
Serial Communication /* * Math is fun! */ int a = 5; int b = 10; int c = 20; void setup () // run once, when the sketch starts { Serial . begin (9600); // set up Serial library at 9600 bps Serial . println ( "Here is some math: " ); Serial . print ( "a = " ); Serial . println (a); Serial . print ( "b = " ); Serial . println (b); Serial . print ( "c = " ); Serial . println (c); Serial . print ( "a + b = " ); // add Serial . println (a + b); Serial . print ( "a * c = " ); // multiply Serial . println (a * c); Serial . print ( "c / b = " ); // divide Serial . println (c / b); Serial . print ( "b - c = " ); // subtract Serial . println (b - c); } void loop () { }
Serial Communication /* * Drive size calculator! */ int drive_gb = 5; int drive_mb ; void setup () // run once, when the sketch starts { Serial.begin (9600); // set up Serial library at 9600 bps Serial.print ( "Your HD is " ); Serial.print ( drive_gb ); Serial.println ( " GB large." ); drive_mb = 1024 * drive_gb ; Serial.print ( "It can store " ); Serial.print ( drive_mb ); Serial.println ( " Megabytes!" ); } void loop () // we need this to be here even though its empty { }
Serial Communication Type Size (bits) Size (bytes) Minimum Value Maximum Value unsigned byte 8 1 255 byte 8 1 -128 127 unsigned int 16 2 65535 int 16 2 -32768 32767 unsigned long 32 4 4294967295 long 32 4 -2147483648 2147483647
Serial Communication /* * Drive size calculator! */ int drive_gb = 100; long drive_mb ; long drive_kb ; long real_drive_mb ; long real_drive_kb ; void setup() // run once, when the sketch starts { Serial.begin (9600); // set up Serial library at 9600 bps Serial.print ("Your HD is "); Serial.print ( drive_gb ); Serial.println (" GB large."); drive_mb = drive_gb ; drive_mb = drive_mb * 1024; drive_kb = drive_mb * 1024; Serial.print ("In theory, it can store "); Serial.print ( drive_mb ); Serial.print (" Megabytes, "); Serial.print(drive_kb); Serial.println (" Kilobytes."); real_drive_mb = drive_gb ; real_drive_mb = real_drive_mb * 1000; real_drive_kb = real_drive_mb * 1000; Serial.print ("But it really only stores "); Serial.print ( real_drive_mb ); Serial.print (" Megabytes, "); Serial.print ( real_drive_kb ); Serial.println (" Kilobytes."); Serial.print ("You are missing "); Serial.print ( drive_kb - real_drive_kb ); Serial.println (" Kilobytes!"); } void loop() // run over and over again { }
Serial Communication (UART communication)
Serial Communication (UART communication) //Transmitter and Recevier int button = 7; int LED = 8; char message; // message character void setup() { Serial.begin (9600); // start communication pinMode (button, INPUT); // input button 1 pinMode (LED, OUTPUT); // input button 2 } void loop() { digitalWrite (LED,LOW); if ( Serial.available ()) // while message received { message = Serial.read (); // read message if (message == '1') // if the message contain character one { digitalWrite (LED, HIGH); // switch led one ON } } if (digitalRead(button)==HIGH) // while button is pushed { Serial.write('1'); // send this Character } delay(20); // delay before sending next message }
I2C Communication
I2C Communication
I2C Communication // Master Code #include < wire.h > int x = 0; void setup() { // Start the I2C Bus as Master Wire.begin (); } void loop() { Wire.beginTransmission (9); // transmit to device #9 Wire.write (x); // sends x Wire.endTransmission (); // stop transmitting x++; // Increment x if (x > 5) x = 0; // `reset x once it gets 6 delay(500); }
I2C Communication // Slave Code #include < Wire.h >
int LED = 13;
int x = 0;
void setup() {
pinMode (LED, OUTPUT); // Define the LED pin as Output Wire.begin (9); // Start the I2C Bus as Slave on address 9 // Attach a function to trigger when something is received. Wire.onReceive ( receiveEvent ); }
void receiveEvent (int bytes) {
x = Wire.read (); // read one character from the I2C
}
void loop() {
if (x == '0') { //If value received is 0 blink LED for 200 ms digitalWrite (LED, HIGH);
delay(200);
digitalWrite (LED, LOW);
delay(200);
} //If value received is 3 blink LED for 400 ms if (x == '3') { digitalWrite(LED, HIGH); delay(400); digitalWrite(LED, LOW); delay(400); } }
SPI Communication (Serial Peripheral Interface)
SPI Communication (Serial Peripheral Interface)
SPI Communication (Serial Peripheral Interface) // Master Code #include < SPI.h > void setup ( void ) { Serial . begin ( 115200 ); //set baud rate to 115200 for usart digitalWrite ( SS , HIGH ); // disable Slave Select SPI . begin (); SPI . setClockDivider ( SPI_CLOCK_DIV8 ); //divide the clock by 8 } void loop ( void ) { char c ; digitalWrite ( SS , LOW ); // enable Slave Select // send test string for ( const char * p = "Hello, world!\r" ; c = * p ; p ++) { SPI . transfer ( c ); Serial . print ( c ); } digitalWrite ( SS , HIGH ); // disable Slave Select delay ( 2000 ); }
SPI Communication (Serial Peripheral Interface) // Slave Code #include < SPI.h > char buff [ 50 ]; volatile byte indx ; volatile boolean process ; void setup ( void ) { Serial . begin ( 115200 ); pinMode ( MISO , OUTPUT ); // have to send on master in so it set as output SPCR |= _BV ( SPE ); // turn on SPI in slave mode indx = ; // buffer empty process = false ; SPI . attachInterrupt (); // turn on interrupt } ISR ( SPI_STC_vect ) // SPI interrupt routine { byte c = SPDR ; // read byte from SPI Data Register if ( indx < sizeof buff ) { buff [ indx ++] = c ; // save data in the next index in the array buff if ( c == '\r' ) //check for the end of the word process = true ;
SPI Communication (Serial Peripheral Interface) } } void loop ( void ) { if ( process ) { process = false ; //reset the process Serial . println ( buff ); //print the array on serial monitor indx = ; //reset button to zero } }
Some Arduino Projects Arduino-based Home Automation System Smart Irrigation Project Human Following Robot Fire Fighter Robot Medicine Reminder using Arduino Arduino-based Weather Station Digital Thermometer using Arduino Arduino-based Security System LED Light Device using Arduino Soil Moisture Monitoring Arduino Automation Systems using Arduino Arduino-based Clock with Alaram Door Lock System with Arduino Arduino Nano BLE 33 Sense Game Controller Color Detection using TCS-3200 and LCD Display Touch Dimmer circuit Precise Motor speed measurement Gas Detector Arduino project Security System using Ultrasonic Sensor Water flow and volume measurement