Week 5: Input & Output device¶
The theme of this week revolved around the “final project” We started by testing different input and output electronics; mostly ones that are relevant to our projects. Then we worked on creating a prototype that at least operates two of the desired functions of the final design.
-
LCD Screen and and IR sensor.
-
LCD screen and Temperature and humidity sensor.
-
connecting two Arduino UNO together using I2C.
-
Creating a prototype of our project.
LCD Screen and and IR sensor¶
This is the IR sensor (infrared red), it consists of two main parts, the IR transmitter and the IR receiver. Basically, the transmitter emits infrared light that propagates through the air until it hits an object. After hitting the object, the light gets reflected towards the IR receiver. The closer the object, the stronger the reflected light will be, hence, the stronger the signal. The sensor has three pins: VCC, GND, and OUT.
This is the connection of the sensor if connected alone without an LCD screen. And here is the direct code to program it. The codes were obtained from this Website
Click to expand
int IRSensor = 9; // connect IR sensor module to Arduino pin D9
int LED = 13; // connect LED to Arduino pin 13
void setup(){
Serial.begin(115200); // Init Serial at 115200 Baud Rate.
Serial.println("Serial Working"); // Test to check if serial is working or not
pinMode(IRSensor, INPUT); // IR Sensor pin INPUT
pinMode(LED, OUTPUT); // LED Pin Output
}
void loop(){
int sensorStatus = digitalRead(IRSensor); // Set the GPIO as Input
if (sensorStatus == 1) // Check if the pin high or not
{
// if the pin is high turn off the onboard Led
digitalWrite(LED, LOW); // LED LOW
Serial.println("Motion Detected!"); // print Motion Detected! on the serial monitor window
}
else {
//else turn on the onboard LED
digitalWrite(LED, HIGH); // LED High
Serial.println("Motion Ended!"); // print Motion Ended! on the serial monitor window
}
}
However, the objective here is to connect it to an LCD screen and print the results on the screen rather than the serial monitor. Therefore, here is the connection of the whole circuit when connected with an LCD screen:
This is the diagram of the wire connection. When following this use a breadboard to connect the LCD screen and the I2C. Also, note that I connected the IR sensor to the 3.3V pin. However, it will work better if connected to the 5V pin. Therefore, connect both the LCD and IR sensor to the 5V using the breadboard.
The same code used above for the IR sensor will be used here with a few changes to make it work with the LCD screen:
Click to expand
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int IRSensor = 2; // connect IR sensor module to Arduino pin D9
int LED = 13; // connect LED to Arduino pin 13
void setup(){
Serial.begin(115200); // Init Serial at 115200 Baud Rate.
Serial.println("Serial Working"); // Test to check if serial is working or not
pinMode(IRSensor, INPUT); // IR Sensor pin INPUT
pinMode(LED, OUTPUT); // LED Pin Output
lcd.init(); // initialize the lcd
lcd.backlight();
}
void loop(){
int sensorStatus = digitalRead(IRSensor); // Set the GPIO as Input
if (sensorStatus == 1) // Check if the pin high or not
{
// if the pin is high turn off the onboard Led
digitalWrite(LED, LOW); // LED LOW
lcd.setCursor(1, 0);
lcd.print("Motion detected"); // print Motion Detected! on the serial monitor window
}
else {
//else turn on the onboard LED
digitalWrite(LED, HIGH); // LED High
lcd.setCursor(1, 0);
lcd.print("Motion ended..."); // print Motion Ended! on the serial monitor window
}
}
Here is a video that showcase the results:
LCD screen and Temperature and humidity sensor¶
This is a composite sensor that can measure the temperature and the humidity, it has 3 pins: VCC, GND, and OUT. This sensor can work with 5V power supply as well as 3.3V.
In this photo you can see the shape of the sensor, the three pins, and their location. If you want to use the sensor alone without the LCD screen, this is the wire connection:
As you can see, the sensor occupies there pins (VCC, GND, and OUT) as mentioned before.
Here is the code that you need to use:
Click to expand
//www.elegoo.com
//2018.10.25
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
/*
* Initialize the serial port.
*/
void setup( )
{
Serial.begin( 9600);
}
/*
* Poll for a measurement, keeping the state machine alive. Returns
* true if a measurement is available.
*/
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );
/* Measure once every four seconds. */
if( millis( ) - measurement_timestamp > 3000ul )
{
if( dht_sensor.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
return( true );
}
}
return( false );
}
/*
* Main program loop.
*/
void loop( )
{
float temperature;
float humidity;
/* Measure temperature and humidity. If the functions returns
true, then a measurement is available. */
if( measure_environment( &temperature, &humidity ) == true )
{
Serial.print( "T = " );
Serial.print( temperature, 1 );
Serial.print( " deg. C, H = " );
Serial.print( humidity, 1 );
Serial.println( "%" );
}
}
Now if you want to print the readings in the LCD screen instead of the serial monitor, follow this connection orientation:
Note that the voltage is connected to the 3.3V, - which can operate the sensor - However, if connected to the 5V pin through the breadboard it will function better.
For the code, we will use the same code as above with few changes to include the LCD screen:
Click to expand
//www.elegoo.com
//2018.10.25
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
/*
* Initialize the serial port.
*/
void setup( )
{
Serial.begin( 9600);
lcd.init(); // initialize the lcd
lcd.backlight(); // Turn on the LCD screen backlight
}
/*
* Poll for a measurement, keeping the state machine alive. Returns
* true if a measurement is available.
*/
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );
/* Measure once every four seconds. */
if( millis( ) - measurement_timestamp > 3000ul )
{
if( dht_sensor.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
return( true );
}
}
return( false );
}
/*
* Main program loop.
*/
void loop( )
{
float temperature;
float humidity;
/* Measure temperature and humidity. If the functions returns
true, then a measurement is available. */
while( measure_environment( &temperature, &humidity ) == true )
{
lcd.setCursor(0, 0);
lcd.print( "T = " );
lcd.print( temperature, 1 );
lcd.print( " deg. C" );
lcd.setCursor(0, 1);
lcd.print( "H = " );
lcd.print( humidity, 1 );
lcd.print( " % " );
delay(3000);
lcd.clear();
}
}
Here is video of the final result:
connecting two Arduino UNO together¶
When me and my group were discussing our project, we stumbled upon few issues. one of them being, that one micro-controller does not have enough pins to operate all the component we want to add in our device. Therefore, we decided to use multiple micro-controllers and connect them together. One of my group mate was trying the possibility of using two Adafruit Feather sense since they have built-in bluetooth while I was tasked to try connecting two Arduino UNO.
The Arduino that we have is Arduino UNO R3 which does not have an built-in bluetooth or Wi-Fi. so to connect them I would need a Bluetooth module or a Wi-Fi module, which was not available at the moment. In addition, adding an extra module will occupy few pins which defeat the purpose. Hence, I found a way to connect two Arduino UNO using wire connections. This is the video that can be watched to guide you through how those components operate. This video explains the method that I will use in detail.
The connection is very easy, all you need is the two Arduinos and thee M-M wires. below are the diagram of the connections:
As you can see, connect the ground of each Arduino to the other Arduino’s ground. Next, connect the (A4) pins together and the (A5) pins together.
Now you have two Arduinos connected together. Both can be connected to the same laptop or can be connected to two separate laptops, it can work either way. One Arduino will be called “The master” and the second Arduino will be called “the slave”. The master, is the one that initiates the orders and send them to the slave. We will use two different codes, the first code will be to the master Arduino and the second code will be to the slave Arduino.
The Master’s code:
Click to expand
/*
I2C Master Demo
i2c-master-demo.ino
Demonstrate use of I2C bus
Master sends character and gets reply from Slave
DroneBot Workshop 2019
https://dronebotworkshop.com
*/
// Include Arduino Wire library for I2C
#include <Wire.h>
// Define Slave I2C Address
#define SLAVE_ADDR 9
// Define Slave answer size
#define ANSWERSIZE 5
void setup() {
// Initialize I2C communications as Master
Wire.begin();
// Setup serial monitor
Serial.begin(9600);
Serial.println("I2C Master Demonstration");
}
void loop() {
delay(50);
Serial.println("Write data to slave");
// Write a charatre to the Slave
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(0);
Wire.endTransmission();
Serial.println("Receive data");
// Read response from Slave
// Read back 5 characters
Wire.requestFrom(SLAVE_ADDR,ANSWERSIZE);
// Add characters to string
String response = "";
while (Wire.available()) {
char b = Wire.read();
response += b;
}
// Print to Serial Monitor
Serial.println(response);
}
The slave’s code:
Click to expands
/*
I2C Slave Demo
i2c-slave-demo.ino
Demonstrate use of I2C bus
Slave receives character from Master and responds
DroneBot Workshop 2019
https://dronebotworkshop.com
*/
// Include Arduino Wire library for I2C
#include <Wire.h>
// Define Slave I2C Address
#define SLAVE_ADDR 9
// Define Slave answer size
#define ANSWERSIZE 5
// Define string with response to Master
String answer = "Hello";
void setup() {
// Initialize I2C communications as Slave
Wire.begin(SLAVE_ADDR);
// Function to run when data requested from master
Wire.onRequest(requestEvent);
// Function to run when data received from master
Wire.onReceive(receiveEvent);
// Setup Serial Monitor
Serial.begin(9600);
Serial.println("I2C Slave Demonstration");
}
void receiveEvent() {
// Read while data received
while (0 < Wire.available()) {
byte x = Wire.read();
}
// Print to Serial Monitor
Serial.println("Receive event");
}
void requestEvent() {
// Setup byte variable in the correct size
byte response[ANSWERSIZE];
// Format answer as array
for (byte i=0;i<ANSWERSIZE;i++) {
response[i] = (byte)answer.charAt(i);
}
// Send response back to Master
Wire.write(response,sizeof(response));
// Print to Serial Monitor
Serial.println("Request event");
}
void loop() {
// Time delay in loop
delay(50);
}
I connected each Arduino to two different laptops to avoid confusion while connecting the boards to the Arduino IDE. However, it will work the same on one Laptop. Here is a video showcasing the results:
In this video, you can see that I programmed the Master Arduino to send a signal to the slave Arduino, and i f the salve Arduino received the signal it sens a back a conformation text “meds dispenser”. You can adjust and change the message to any message you want.
Creating a Prototype of the project¶
The documentation of the Prototype is done in a separate section, you can access it by clicking Here