Electronics Design Workshop Chapter 3 Electronic circuit building blocks including common sensors Satya P. Singh Electronics Design Workshop 3.1 Introduction Electronic circuit building blocks include various components such as resistors, capacitors, transistors, diodes, and integrated circuits. Fig. 3.1. Different types of Sensors. The Figure is copied from https://circuitdigest.com/tutorial /different-types-of-sensors-andtheir-working Common sensors used in electronic circuits include: • Temperature sensors: These measure the temperature of a system and can be used in a variety of applications such as HVAC systems and refrigeration. • Light sensors: These measure the intensity of light and can be used in applications such as automatic lighting control and camera exposure control. • Proximity sensors: These detect the presence of objects and can be used in applications such as automatic door openers and mobile phone touchless gesture control. • Motion sensors: These detect movement and can be used in applications such as security systems and fitness trackers. • Sound sensors: These measure the intensity of sound and can be used in applications such as noise level monitoring and speech recognition. • Pressure sensors: These measure the force exerted by a gas or liquid and can be used in applications such as tire pressure monitoring systems and blood pressure monitoring. • Humidity sensors: These measure the amount of moisture in the air and can be used in applications such as weather monitoring and indoor air quality control. Satya P. Singh Electronics Design Workshop • Accelerometer sensors: These measure the acceleration or tilt of an object and can be used in applications such as mobile phones, gaming devices, and vibration monitoring systems. 3.2 Arduino programming and use Arduino is an open-source electronics platform that includes both hardware and software. It is intended for anyone interested in creating interactive objects or environments. The Arduino platform includes a wide range of boards, each with its own set of features and capabilities, as well as a variety of software libraries that make it easy to interact with sensors, actuators, and other devices. Arduino boards are widely used in various fields like engineering, robotics, IoT and many more. It is considered as a beginner-friendly platform for learning electronics and programming. Arduino programming is done using the Arduino Software (IDE) which is available for free to download for Windows, Mac OS X, and Linux. It is a simple Integrated Development Environment (IDE) that allows you to write, upload, and run code on an Arduino board. The Arduino IDE uses a simplified version of C++, making it easy for beginners to learn. The environment includes a text editor for writing code, a message area for feedback and error messages, and a toolbar with buttons for uploading code and controlling the board. Some popular projects that can be built using Arduino include: • • • • • • Blinking an LED Reading a temperature sensor Controlling a servo motor Building a remote control car Building a line following robot Building a weather station Satya P. Singh Electronics Design Workshop 3.3 Getting acquainted with the Arduino IDE and Basic Sketch structure The basic structure of an Arduino program is called a sketch. A sketch is composed of two main functions: setup() and loop(). The setup() function is executed once when the board is powered on or reset, and is used to configure the board's input and output pins, and initialize variables. The loop() function is executed repeatedly and is used to read input from sensors, control output to actuators, and perform other tasks. Here is an example of a simple Arduino sketch that blinks an LED: 1 2 3 4 5 6 7 8 9 10 void setup() { pinMode(13, OUTPUT); // configure pin 13 as an output } void loop() { digitalWrite(13, HIGH); // turn on the LED delay(1000); // wait for 1 second digitalWrite(13, LOW); // turn off the LED delay(1000); // wait for 1 second } This sketch configures the board's pin 13 as an output in the setup() function, and then repeatedly turns the LED on and off with a 1-second delay in the loop() function. Arduino programming also allows you to use a variety of libraries that provide prewritten code for common tasks such as reading sensors, controlling motors, and communicating over networks. These libraries can save a lot of time and make it easy to add advanced functionality to your projects. 3.4 Digital Input and output Digital Input and Output (I/O) in Arduino refers to the process of reading or writing a digital signal, which can only have two states: HIGH or LOW (1 or 0). In Arduino, digital I/O is accomplished using digital pins on the board. Digital Output: To write a digital value to a pin, you can use the digitalWrite() function. This function takes two parameters: the pin number and the value you want to write (HIGH or LOW). For example, to set pin 13 to HIGH: 1 digitalWrite(13, HIGH); Digital Input: To read a digital value from a pin, you can use the digitalRead() function. This function takes one parameter: the pin number. It returns the value of the pin (HIGH or LOW). For example, to read the value of pin 2: 1 int buttonState = digitalRead(2); Satya P. Singh Electronics Design Workshop It is important to note that before using a pin as input, you must set its mode using the pinMode() function. For example, to set pin 2 as an input: 1 pinMode(2, INPUT); Additionally, you can also use the pull-up or pull-down resistors to set the default state of the input pin, when it is not connected to anything. For example, to enable the internal pull-up resistor on pin 2: 1 pinMode(2, INPUT_PULLUP); In summary, to use a pin as a digital input or output in Arduino, you should set the pin mode using pinMode(), read the digital value using digitalRead() or write the digital value using digitalWrite(). 3.5 Measuring time and events In Arduino, you can measure time and events using a variety of built-in functions and libraries. Here are a few common examples: 1. delay(): This function causes the program to pause for a specified number of milliseconds. For example, the following code will turn on an LED for one second: 1 digitalWrite(13, HIGH); 2 delay(1000); 3 digitalWrite(13, LOW); 2. millis(): This function returns the number of milliseconds that have passed since the Arduino board began running the current sketch. This can be useful for measuring how much time has passed since a certain event occurred. For example, the following code will blink an LED once per second: 1 2 3 4 5 6 7 8 9 10 unsigned long previousMillis = 0; const long interval = 1000; void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; digitalWrite(13, !digitalRead(13)); } } 3. micros(): This function is similar to millis(), but it returns the number of microseconds that have passed since the Arduino board began running the current sketch. 4. Timer/Counter: Arduino boards have built-in timer/counters that can be used to generate timed events or measure the frequency of input signals. For Satya P. Singh Electronics Design Workshop example, you can use the Timer library to create a timer that calls a function after a specified interval. 5. Interrupts: Arduino boards also support external interrupts, which allow you to detect specific events and respond to them immediately, rather than waiting for the loop() function to check for them. For example, you can use the attachInterrupt() function to call a function when a button is pressed. In summary, measuring time and events using Arduino can be done using built-in functions like delay(), millis() and micros(). You can also use libraries and timer/counter or interrupts to achieve this. 3.6 Pulse Width Modulation PWM, or Pulse Width Modulation, is a technique used to control the amount of power delivered to a device by rapidly turning the power on and off. On the Arduino platform, this can be achieved by using the analogWrite() function. This function takes two arguments: the first is the pin number to which the PWM signal will be sent, and the second is the duty cycle of the PWM signal, represented as a number between 0 and 255. For example, to send a PWM signal with a duty cycle of 128 (50% power) to pin 9, you would use the following code: 1 analogWrite(9, 128); In addition to the analogWrite() function, you can use the tone() function to generate PWM signals to drive speakers or other devices. Here is an example of Arduino code that uses PWM to control the brightness of an LED connected to pin 9: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 void setup() { // Set pin 9 as an output pinMode(9, OUTPUT); } void loop() { // Gradually increase the brightness of the LED for (int i = 0; i < 255; i++) { analogWrite(9, i); delay(10); } // Gradually decrease the brightness of the LED for (int i = 255; i > 0; i--) { analogWrite(9, i); delay(10); } } Satya P. Singh Electronics Design Workshop 3.7 Serial communication in arduino Serial communication is a method of transmitting data one bit at a time, over a communication channel. In the case of Arduino, the Serial library provides an easy way to send and receive data over the serial port. Here is an example of Arduino code that uses the Serial library to send data from the Arduino board to a computer: 1 2 3 4 5 6 7 8 9 10 void setup() { // Set the baud rate for serial communication Serial.begin(9600); } void loop() { // Send a message over the serial port Serial.println("Hello, world!"); delay(1000); } In this code snippet, the setup() function calls the Serial.begin() function, which sets the baud rate for serial communication. The loop() function uses the Serial.println() function to send the string "Hello, world!" over the serial port and the delay(1000) function is used to create a delay of 1 second between each iteration of the loop. On the other hand, here is an example of Arduino code that uses the Serial library to receive data from a computer and control an LED: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 const int ledPin = 13; void setup() { // Set the baud rate for serial communication Serial.begin(9600); pinMode(ledPin, OUTPUT); } void loop() { if (Serial.available() > 0) { int data = Serial.read(); if (data == '1') { digitalWrite(ledPin, HIGH); } else if (data == '0') { digitalWrite(ledPin, LOW); } } } In this code snippet, the setup() function calls the Serial.begin() function and sets the baud rate for serial communication and also sets the led pin as output. The loop() function uses the Serial.available() function to check if there is data available in the serial buffer, and if so, it reads the data using the Serial.read() function and check if Satya P. Singh Electronics Design Workshop it's 1 or 0, if it is 1 it turn on the led using the digitalWrite(ledPin, HIGH) function, if it's 0 it turn off the led using the digitalWrite(ledPin, LOW) function. It's important to note that the baud rate must match the baud rate set in the serial monitor or other serial communication software that you are using to communicate with the Arduino. 3.8 Analog input in arduino Analog input in Arduino refers to the process of reading an analog voltage value and converting it into a digital value that can be processed by the microcontroller. The Arduino board has a built-in analog-to-digital converter (ADC) that can be used to read analog input. Here is an example of Arduino code that uses the analogRead() function to read the voltage on an analog pin and convert it to a digital value: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 const int analogPin = A0; void setup() { // Initialize the serial communication Serial.begin(9600); } void loop() { // Read the analog value on pin A0 int sensorValue = analogRead(analogPin); // Print the value over the serial port Serial.println(sensorValue); delay(1000); } In this code snippet, the setup() function calls the Serial.begin() function to initialize the serial communication at 9600 baud rate. The loop() function uses the analogRead(analogPin) function to read the voltage on pin A0 and store the value in the sensorValue variable. The Serial.println(sensorValue) function is then used to send the value over the serial port, where it can be read using a serial monitor or other serial communication software. The delay(1000) function is used to create a delay of 1 second between each iteration of the loop. It's important to note that the analogRead() function returns a value between 0 and 1023, where 0 corresponds to 0V and 1023 corresponds to the voltage of the reference voltage (5V for most boards). Also, it's important to mention that the number of the analog input pin varies depending on the type of the board, for example, in Arduino Uno board A0 is the first analog pin, in Arduino Mega board A0 is the first analog pin and so on. Satya P. Singh Electronics Design Workshop 3.9 Interrupts programming Interrupts in Arduino are a feature that allows the microcontroller to stop executing the main program and execute a special function called an interrupt service routine (ISR) when a certain event occurs, such as a button press or a sensor reading. Interrupts are useful for handling events that need immediate attention and prevent the microcontroller from wasting time polling for the event to occur. Here is an example of Arduino code that uses an interrupt to handle a button press: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 const int buttonPin = 2; const int ledPin = 13; void setup() { pinMode(buttonPin, INPUT_PULLUP); pinMode(ledPin, OUTPUT); // Attach the ISR to the interrupt attachInterrupt(digitalPinToInterrupt(buttonPin), handleButton, FALLING); } void loop() { // Do something else here } // ISR function void handleButton() { digitalWrite(ledPin, !digitalRead(ledPin)); } In this code snippet, the setup() function sets the button pin as an input with a pullup resistor enabled, and sets the led pin as an output. The attachInterrupt(digitalPinToInterrupt(buttonPin), handleButton, FALLING) function is used to attach the ISR handleButton() to the interrupt of the button pin, and the FALLING edge of the input is selected as the trigger, this means that the interrupt will be triggered when the button is pressed and the input goes low. The loop() function can be used to do something else in the meantime. The handleButton() function is the ISR and it will be executed when the interrupt is triggered, in this case it toggles the state of the led pin. It's also possible to specify different triggers of the interrupt, such as RISING or CHANGE, and also specify different priorities for the interrupt with the priority parameter. It's important to note that the ISR function should be as short as possible and should not contain any blocking or long-running code, as this can cause the microcontroller to miss other interrupts or cause the system to become unresponsive. Satya P. Singh Electronics Design Workshop Exercise: 1. What are electronic circuit building blocks, and how are they used to design complex electronic systems? Explain with the help of suitable examples. 2. Discuss the basics of Arduino programming and its use in electronic projects. Explain the significance of the Arduino IDE and Basic Sketch structure. 3. How can digital input and output be used in electronic circuits? Provide examples of applications where digital input and output are used extensively. 4. What are PWM signals, and how are they used to control electronic devices? Explain with suitable examples. 5. Discuss the importance of serial communication in electronic systems. What are the different types of serial communication, and how are they used? 6. What is analog input, and how is it used in electronic circuits? Provide examples of analog input applications. 7. Explain the concept of interrupts programming and its significance in electronic systems. Provide examples of applications where interrupts are commonly used. Satya P. Singh