This is my original work. The notes I have made on some pages were learnt over time, you can incorporate them in your electronics hobbying (although this site is mostly used for copying projects and not learning haha). Feel free to use any part of the project.
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
BTech ECE project final report using Arduino
1. AUTOMATIC SWITCHING SYSTEM FOR HOME/INDUSTRIAL APPLICATIONS
1
AUTOMATIC SWITCHING SYSTEM FOR
HOME/INDUSTRIAL APPLICATIONS
(TEC-490B)
PROJECT REPORT
SUBMITTED TO:
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING
COLLEGEOF TECHNOLOGY,
G.B.PANT UNIVERSITY OF AGRICULTURE AND TECHNOLOGY
PANTNAGAR-263145,UTTARAKHAND,INDIA
BY:
BhuvneshKhantwal(52000)
Aayush Dangwal(51991)
Vikas Singh Rana (52017)
GUIDED BY:
Dr. Sanjay Mathur
Dr. Pushpendra Gupta
Mr. Rishi Nigam
FOR THE DEGREE OF BACHELOR OF TECHNOLOGY
(JULY,2021)
2. AUTOMATIC SWITCHING SYSTEM FOR HOME/INDUSTRIAL APPLICATIONS
2
ACKNOWLEDGEMENT
We would like to thank our project mentors Dr. Sanjay Mathur, Dr. Pushpendra
Gupta, Mr. Rishi Nigam for guiding us through this project.
We are also grateful for the help from the professor John Valvano and Yeraballi
from UT Austin for sharing their invaluable experience with us through piazza .
Bhuvnesh, Aayush and Vikas
12 July, 2021
3. 3
ABSTRACT
In modern world, automation is solving many of human’s problems and taking out
the burden of doing things manually, especially the repetitive tasks. Automatic
switching systems are one of the subsets in this domain which are now being
installed in all the different fields for making the systems switch on their own
without much of human intervention.These systems not only facilitate tension free
operation but also make the operations more efficient and accurate.The presented
project is a switching system designed with a view to operate in real life conditions;
user enters the time at which switching is to occur. The timing schedule can be
extended to a daily basis, or weekly basis or even monthly basis. Apart from that
different modes of operations areprogrammed and user can switch between these
modes. Hence, the product can handle a number of switching requirements. This
project has myriad of scope in all the diferent home and industrial appliances. For
example submersible pump control, street lights, office lights, applications requiring
periodic switching where a person cannot always be present.
4. AUTOMATIC SWITCHING SYSTEM FOR HOME/INDUSTRIAL APPLICATIONS 4
CONTENTS
Title Page
ACKNOWLEDGEMENT 2
ABSTRACT 3
INTRODUCTION
5
FLOW IN PROJECT PROCESS
CIRCUIT DIAGRAM
FLOW OF CONTROL
THEORY
ATMEGA 328
LCD1602
RTC MODULE
EEPROM OF ATMEGA328
RELAY
TACTILE SWITCH
7805IC
RAM
7
11
12
15
16
18
19
20
20
22
22
23
FUTURE SCOPE 27
REFERENCES 28
5. AUTOMATIC SWITCHING SYSTEM FOR HOME/INDUSTRIAL APPLICATIONS 7
INTRODUCTION
Nowadays automation systems have become widespread in several industries by
playing a vital role in dominating many process-related operations. We live in the
world of automation wherein most of the systems have become machine-driven,
such as industrial automation, automation in homes and alternative business sectors.
Home automation systems advancing towards mechanization processes whereby
less human efforts are required by the machinery equipment to control numerous
systems in homes. It involves automatic controlling of home appliances using
completely different technologies and controllers over desktops, laptops good
phones or tablets. Automation systems are classified into two types: industrial
automation system and home automation systems. Automation systems are
classified into two types such as industrial automation system and home automation
systems. Home automation systems are further classified into three types: Power
line Based Home Automation Wired or BUS Cable Home Automation Wireless Home
Automation.
Benefits of Smart Home Automation
Flexibility: Your automatic appliances work to make your life easier, and it can
be customized as your needs change. So many appliances are compatible with
each other, and your system allows you to easily add or remove appliances as
you see fit.
Peace of Mind: The peace of mind smart home installation provides is worth
every penny. You can leave the home knowing everything will be safe and
sound when you return.
More Energy Efficient
Convenience: Imagine already preheated oven or waking up to fresh-brewed
coffee. With automation, these dreams can be your reality.[1]
6. AUTOMATIC SWITCHING SYSTEM FOR HOME/INDUSTRIAL APPLICATIONS 8
1 Some previously available switching systems and their drawbacks
1. Very less switching options/variables
Most of the switching systems available online have very limited switching features,
good for only very narrow range of applications. 9
2. Lower power switching
Most of the systems are limited to be used for low power appliances.
3. Reliability issues
The biggest issue from the product reviews turns out to be their reliability and life.
Most of these systems, especially low-cost ones are too poor to be relied.[2]
4. Only one device can be switched
Most of the products had only one switching outlet which mean that many such
devices have to be bought to use multiple appliances.[3]
7. AUTOMATIC SWITCHING SYSTEM FOR HOME/INDUSTRIAL APPLICATIONS
The flow in project progress
switching systems
AUTOMATIC SWITCHING SYSTEM FOR HOME/INDUSTRIAL APPLICATIONS
The flow in project progress
Progression in
making better
switching systems
Switching System
based on LDR
Switching system
using Internal
Interrupts of a
microcontroller
RTC based Switching
System
10
8. AUTOMATIC SWITCHING SYSTEM FOR HOME/INDUSTRIAL APPLICATIONS 11
LDR based switching system:
Designed a product for a real consumer
which switched the light of his office ON
at day and OFF automatically at night. The
project did not use a Microcontroller.
Designed a Transistor circuit with Relay;
calculated and tested Various Resistance
values for correct switching at intended
Light Intensity.
The operation of switching could be
changed to Manual Mode or Automatic
Mode.
In manual mode the switching depends
on the state on a switch (controlled by
the user)
In automatic mode, the transistor circuit
automatically does switching based on
the sunlight intensity.
Potentiometer was added for further
control over automatic switching.
No external DC source was required for
operation of circuitry.
Just plug in the AC mains which is to be
controlled and the required DC power is
generated from this AC line.
For converting AC to DC a DC adopter
was fitted inside the case.
External connection with the AC lines is
made using three pin screw terminal at the side of the box.
The project had some issues so a better version of this project is under development
(Timer Based Switch) [4]
VIDEOLINK: https://www.youtube.com/watch?v=4sAVVNPtRgc&t=309s
9. AUTOMATIC SWITCHING SYSTEM FOR HOME/INDUSTRIAL APPLICATIONS 12
Timer Interrupt Based Switching:
Aimed particularly at developing an
automatic Lighting System using Relay for
situations where a user cannot be
available at all times for switching
manually. The microcontroller IC used is
ATmega328 (Under development)
User Interface includes a 16*2 LCD screen
and tactile buttons.
The interface lets the user Set the three
times, ON time, OFF time, START AFTER
times in HH:MM:SS format.
The sequence of ON and OFF time starts
after ‘START AFTER time’
Switch Bounce eliminated by using delay
using TIMER2 Interrupt of ATmega328.
TIMER1 Interrupt is responsible for the
actual Timer operation.
The currently set time values are stored in
the EEPROM for future use.
On reset, the user can set a new time or
use the previous set time (stored in
EEPROM) for switching.
A few sample times are already set in EEPROM to readily choose from.
A buzzer buzzes at every switching.
An easy to use function was made for customizing buzzing duration as well as frequency
of the buzzer.[5]
Drawback: The system loses track of time in case of power cut
10. AUTOMATIC SWITCHING SYSTEM FOR HOME/INDUSTRIAL APPLICATIONS 13
Salient features of the newly proposed Project:
System will be able to differentiate between a power cut and a Manual Reset.
(Using Watchdog timer)
In case of power cut, the microcontroller will continue operation as soon as the
power is ON. It does not lose track of time.
In the case of manual reset, the screen will prompt user to enter new switching
time values. (These values are automatically saved in EEPROM for using them in
case of a power cut.)
The switching operation switches the state of a 5V Relay
This relay drives a contractor to achieve high power switching.
Major Components used in the project:
LCD1602
Atmega328
Contractor
RTC module
Power regulator
Potentiometer
Piezo buzzer
Resistors, Capacitors
Tactile Switches
Relay
Zero PCB
12. AUTOMATIC SWITCHING SYSTEM FOR HOME/INDUSTRIAL APPLICATIONS 15
Flow of Control
System opens
System was reset
System asks user for
Manual(Mode0)/Preset(Mode1)
mode
Mode=0
System asks for Mode2 or
Mode 3
Mode=2
Use last used
time
Retrieve
switching
values from
Mode=3
Set new time by
user
Prompts user
to set time
Save these
values to
EEPROM
Mode=1
Set Timer to a
predefined
default timing
value
Save to EEPROM
Power cut had
happened
Retrieve
switching
values from
EEPROM to
variables
System checks if the system was reset or
shut down due to power off
13. A note of over the time learning during project development
Earlier, we sought tactile switch as an option - tried removing bounce . Eventually we
saw potentiometers as better optionsbut they had limited rotation plus shows
absolute rather than relative value .So, we came up with idea to use rotatory
encoder,but the bounce had to be removed here too.
………………………………………………………………………………………
We used interrupt timer1 for basic counting to remove debounce, rather than use a
capacitor, we used software to do so.
We disarmed the int0 and int1 interrupts inside them, then activated them again
inside the counter2 after a certain delay.
After a long struggle with volatile keyword we finally found its correct
implementation. We understand volatile does not lets a variable update its value but
if it is a pointer then, it at least asks the system to re-read it. however, re reading a
variable would not give any value change if it has already been defined but if it is a
pointer then certainly it is not already defined and reading it again and again can give
a changed value.
Also, variables inside an ISR are usually defined with the volatile data specifier.
Timer0 wasn’t used for delay () function to work properly.
………………………………………………………………………………………
We were wondering how to change states of hr. min and sec,we used finite state
machine mealy model and tackled the problem.
………………………………………………………………………………………
We then came up with a mindboggling problem of automatic reset of the perfboard
built circuit. Even this fact came to realize after a lot of mind scratching and blaming
other parts of the circuit and even software. We came up with using caps across
7805 IC and learnt a lot about ceramic and electrolytic caps.
………………………………………………………………………………………
We realized that Serial Monitor communication(UART) is rather slow speed so it has
some time delay before reading the next value. The serial communication puts the
retrieved values in a buffer, and they do not get erased from buffer till it is read. The
fact that this serial communication sends a last character as newline into the buffer
can be seen from seeing its integer value which is 10. Otherwise in character form it
just gives a line break.
………………………………………………………………………………………
Then we tried making changes in slave's code flow depending on the master's
instructions. Started looking for different options like goto, resetting the arduino(by
using code and a digital pin).
We learned about memory mapping of atmega 328 and various types of typical
memories on a microcontroller(flash(program and bootloader memory), sram(data
memory) and eeprom(extra memory for user to write permanently)). Then about
how to easily write to eeprom using built in eeprom.h library. Also about hazards of
writing many times to the same memory address.
14. The ATmega328 chip found on the Uno has the following amounts of memory:
Flash 32k bytes (of which .5k is used for the bootloader)
RAM( it uses SRAM) 2k bytes
EEPROM 1k byte
We learned about SRAM’s allocation of Variables i.e Static, Global, Local Variables
(doesn’t take SRAM used when function exists)
………………………………………………………………………………………
Started working on sim800L thinking if its addition would make the project even
better .The module works on 3.3V logic and hence directly connecting to arduino can
damage it so instead use a voltage divider on Rx pin of SIM800L to get 5V down to
about 3.3V. Also, SIM800L works on about 3.4 to 4.4V so we used buck convertor to
power it.
………………………………………………………………………………………
The Hardware serial (pin 0 and 1) on Atmega328 is for serial(UART) communication
with the computer) and applied by using “Serial.xxxxxx() ;” commands. There is no
need to #include any library for this hardware Serial(UART). While any other
device(module) may be using the UART to communicate with Atmega328. For that
we cannot use these hardware serial ports because it would interfere while
uploading any program or printing on serial monitor both of which uses this
hardware serial port. So we #include SoftwareSerial.h header to define one more
pair of Rx and Tx pins other than pins 0 and 1. We first make an object as
“SoftwareSerialmynewserial(RXPin, TXPin);” Now we can use mynew serial to be
used for sending and receiving data from any other serial(UART) module like GSM,
GPS module etc.
Example: while (mynewserial.available() > 0) {Serial.write(mynewserial.read());}
15. AUTOMATIC SWITCHING SYSTEM FOR HOME/INDUSTRIAL APPLICATIONS 20
THEORY
ATMEGA328P EXTERNAL INTERRUPT
SENSE CONTROL
The INT0 and INT1 interrupts can be triggered by a low logic level, logic change, and
a falling or rising edge.
External Interrupt Control Register A
This is set up as indicated in the specification for the External Interrupt Control
Register
ISCN1 ISCN0
ARDUINO
MODE DESCRIPTION
0 0 LOW
The low level of
INTn generates an
interrupt request
0 1 CHANGE
Any logical change
on INTn generates
and interrupt
request
1 0 FALLING
The falling edge of
INT0 generates an
interrupt request
1 1 RISING
The rising edge of
INT0 generates an
interrupt request
ATmega328P External Interrupt Enable
All interrupts are assigned individual enable bits which must be written logic one
together with the Global Interrupt Enable bit in the Status Register (SREG) in order to
enable the interrupt.
16. Status Register
The ATmega 328P supports two external interrupts which are individually enabled by
setting bits INT1 and INT0 in the External Interrupt Mask Register
External Interrupt Mask Register
When an edge or logic change on the INT0 pin triggers an interrupt request, INTF0
becomes set (one). If the I-bit in SREG and the INT0 bit in EIMSK are set (one), the
MCU will jump to the corresponding Interrupt Vector. The flag is cleared when the
interrupt routine is executed.
External Interrupt Flag Register
Alternatively, the flag can be cleared by writing a logical one to it. The EIFR register is
within the I/O address range (0x00 to 0x1F) of the Set Bit in I/O Register (SBI)
Instruction. This flag is always cleared when INT0 is configured as a level interrupt.[6]
PINOUT of ATMEGA328
The Atmega328 is a very popular microcontroller chip produced by Atmel. It is an 8-
bit microcontroller that has 32K of flash memory, 1K of EEPROM, and 2K of internal
SRAM.
The Atmega328 is one of the microcontroller chips that are used with the popular
Arduino Duemilanove boards. The Arduino Duemilanove board comes with either 1 of
2 microcontroller chips, the Atmega168 or the Atmega328. Of these 2, the Atmega328
is the upgraded, more advanced chip. Unlike the Atmega168 which has 16K of flash
program memory and 512 bytes of internal SRAM, the Atmega328 has 32K of flash
program memory and 2K of Internal SRAM.
17. The Atmega328 has 28 pins.
It has 14 digital I/O pins, of which 6 can be used as PWM outputs and 6 analog input
pins. These I/O pins account for 20 of the pins.[7]
The table below gives a description for each of the pins, along with their function.
Pin
Number
Description Function
1 PC6 Reset
2 PD0
Digital Pin
(RX)
3 PD1
Digital Pin
(TX)
4 PD2 Digital Pin
5 PD3
Digital Pin
(PWM)
6 PD4 Digital Pin
7 Vcc
Positive
Voltage
(Power)
8 GND Ground
9 XTAL 1
Crystal
Oscillator
10 XTAL 2
Crystal
Oscillator
11 PD5
Digital Pin
(PWM)
18. 12 PD6
Digital Pin
(PWM)
13 PD7 Digital Pin
14 PB0 Digital Pin
15 PB1
Digital Pin
(PWM)
16 PB2
Digital Pin
(PWM)
17 PB3
Digital Pin
(PWM)
18 PB4 Digital Pin
19 PB5 Digital Pin
20 AVCC
Positive
voltage for
ADC (power)
21 AREF
Reference
Voltage
22 GND Ground
23 PC0 Analog Input
24 PC1 Analog Input
25 PC2 Analog Input
26 PC3 Analog Input
27 PC4 Analog Input
28 PC5 Analog Input
LCD1602
LCD1602, or 1602 character-type liquid crystal display, is a kind of dot matrix module
to show letters, numbers, and characters and so on. It's composed of 5x7 or 5x11 dot
matrix positions; each position can display one character. There's a dot pitch between
two characters and a space between lines, thus separating characters and lines. The
model 1602 means it displays 2 lines of 16 characters.[8]
19. RTC module
RTC modules are simply TIME and DATE remembering systems which have battery
setup which in the absence of external power keeps the module running. This keeps the
TIME and DATE up to date. So we can have accurate TIME and DATE from RTC
module whenever we want.[9]
Pin Name Description
VCC Connected to positive of power source.
GND Connected to ground.
SDA Serial Data pin (I2C interface)
SCL Serial Clock pin (I2C interface)
20. SQW Square Wave output pin
32K 32K oscillator output
EEPROM of ATMEGA328
The microcontroller on the Arduino and Genuino AVR based board has EEPROM:
memory whose values are kept when the board is turned off (like a tiny hard drive).
This library enables you to read and write those bytes.
The supported micro-controllers on the various Arduino and Genuino boards have
different amounts of EEPROM: 1024 bytes on the ATmega328P, 512 bytes on
the ATmega168 and ATmega8, 4 KB (4096 bytes) on
the ATmega1280 and ATmega2560. The Arduino and Genuino 101 boards have an
emulated EEPROM space of 1024 bytes.
Storing values in the flash memory of your microcontroller is easy and fast : simply
define any variable and assign it a value, you're done. But hat happens if your µC is
reset ?
EEPROM (Electrically-Erasable Programmable Read-Only Memory) is a persistent
memory that allows you to store up to 1024 bytes (1 kilobyte) in your
microncontroller, even when it's turned off.
Arduino offers a native EEPROM library that allows us to easily deal with the
EEPROM of the ATMega328 (or whatever Atmel µC your Arduino is running).
[10][11]
Relay
A relay is an electrically operated switch. It consists of a set of input terminals for a
single or multiple control signals, and a set of operating contact terminals. The switch
may have any number of contacts in multiple contact forms, such as make contacts,
break contacts, or combinations thereof.
Relays are used where it is necessary to control a circuit by an independent low-power
signal, or where several circuits must be controlled by one signal. Relays were first
used in long-distance telegraph circuits as signal repeaters: they refresh the signal
coming in from one circuit by transmitting it on another circuit. Relays were used
extensively in telephone exchanges and early computers to perform logical operations.
The traditional form of a relay uses an electromagnet to close or open the contacts, but
other operating principles have been invented, such as in solid-state relays which
21. use semiconductor properties for control without relying on
calibrated operating characteristics and sometimes multiple operating coils are used to
protect electrical circuits from overload or faults; in modern electric power systems
these functions are performed by digital instruments still called
Working
Relay works on the principle of electromagnetic induction.
When the electromagnet is applied with some current it induces a magnetic field
around it.
Above image shows working of the relay .A switch is used to apply DC current to the
load.
In the relay Copper coil and the iron core acts as electromagnet.
properties for control without relying on moving parts
erating characteristics and sometimes multiple operating coils are used to
protect electrical circuits from overload or faults; in modern electric power systems
these functions are performed by digital instruments still called protective relays
Relay works on the principle of electromagnetic induction.
When the electromagnet is applied with some current it induces a magnetic field
working of the relay .A switch is used to apply DC current to the
In the relay Copper coil and the iron core acts as electromagnet.
moving parts. Relays with
erating characteristics and sometimes multiple operating coils are used to
protect electrical circuits from overload or faults; in modern electric power systems
protective relays.[12]
When the electromagnet is applied with some current it induces a magnetic field
working of the relay .A switch is used to apply DC current to the
22. When the coil is applied with DC current it starts attracting the contact as shown. This
is called energizing of relay.
When the supply is removed it retrieves back to the original position. This is called De
energizing of relay.
There are also such relays, whose contacts are initially closed and opened when there is
supply i.e. exactly to opposite to the above shown relay.[13][14]
Tactile Switch
These small sized switches are placed on PCBs and are used to close an electrical
circuit when the button is pressed by a person.
When the button is pressed, the switches turn ON and when the button is released, the
switches turn OFF.
A tactile switch is a switch whose operation is perceptible by touch.
Tactile switches produce a bump of varying sizes (depending on the switch) and emit a
small clicky sound. The tactile switches are not quite as loud as a clicky switch, so you
should be able to use them in a public setting without drawing too much attention to
yourself with the noise.
Tactile switches have a small tactile bump that provides resistance which can be felt at
the point of key actuation. The switch itself is practically inaudible, omitting the click
sound present in clicky switches. Tactile switches are versatile performers that cope
well with a variety of different typing tasks.[15]
2n2222A Transistor
The 2N2222A transistor is very much similar to the commonly used NPN transistor
BC547. But there are two important features that distinguish both. 2N2222A can allow
23. collector current upto 800mA and also has power dissipation of 652mW which can be
used to drive larger loads than compared with BC547.
So if you looking for an NPN transistor that could switch loads of higher current
then 2N2222A might the right choice for your project.
7805 IC
Voltage sources in a circuit may have fluctuations resulting in not providing fixed
voltage outputs. A voltage regulator IC maintains the output voltage at a constant
value. 7805 Voltage Regulator, a member of 78xx series of fixed linear voltage
regulators used to maintain such fluctuations, is a popular voltage regulator integrated
circuit (IC).
The xx in 78xx indicates the output voltage it provides. 7805 IC provides +5 volts
regulated power supply with provisions to add a heat sink.
Input voltage range 7V- 35V
Current rating Ic = 1A
Output voltage range VMax=5.2V ,VMin=4.8V
IC 7805 is a 5V Voltage Regulator that restricts the output voltage to 5V output for
various ranges of input voltage. It acts as an excellent component against input voltage
fluctuations for circuits, and adds an additional safety to your circuitry.[16]
Random Access Memory: types and
differences
24. There are two main types of RAM available in embedded devices:
random access memory) and DRAM
is faster in read/write/access operation, it is also more expensive and usually takes
more physical space. On the other hand, DRAM is generally slower in
read/write/access operations (this improves with each gene
produce and usually smaller with respect to its physical size.
No matter which type of RAM (SRAM or DRAM) uses
following discussion stands. Many of the MCUs used by Arduino boards (e.g.,
ATmega328p in Arduino UNO v3
SRAM memory, but unfortunately only in small quantities
ATmega328P and 8KB for ATmega2560), thus special care is required in writing the
code. For the rest of the discussion, we only use the RA
DRAM.[17][18]
RAM Diagnose: when
At first, we need to check if the problem is caused by insufficient free RAM,
by various other possible reasons, such as a defective MCU, problem with peripherals
or even non-obvious code bugs. Debugging an Arduino is not really easy since it does
not "beeps" on error, does not show blue screens and also does not trigger popup
windows telling you which is the possible problem. The RAM available in an Arduino
MCU is organized as shown in the picture below (picture linked from:
.data variables is the first RAM section and it is used to store program static data, such
as strings, initialized structures and global variables.
.bss variables is the memory allocated for uninitialized global and static variables.
There are two main types of RAM available in embedded devices:
DRAM (dynamic random access memory). While SRAM
is faster in read/write/access operation, it is also more expensive and usually takes
more physical space. On the other hand, DRAM is generally slower in
read/write/access operations (this improves with each generation), but cheaper to
produce and usually smaller with respect to its physical size.
No matter which type of RAM (SRAM or DRAM) uses an embedded device, the
following discussion stands. Many of the MCUs used by Arduino boards (e.g.,
UNO v3 and ATmega2560 in Arduino MEGA2560
SRAM memory, but unfortunately only in small quantities (e.g., 2KB for
ATmega328P and 8KB for ATmega2560), thus special care is required in writing the
code. For the rest of the discussion, we only use the RAM term for both, SRAM and
RAM Diagnose: when heap meets stack
the problem is caused by insufficient free RAM,
by various other possible reasons, such as a defective MCU, problem with peripherals
obvious code bugs. Debugging an Arduino is not really easy since it does
not "beeps" on error, does not show blue screens and also does not trigger popup
windows telling you which is the possible problem. The RAM available in an Arduino
as shown in the picture below (picture linked from: avr
is the first RAM section and it is used to store program static data, such
as strings, initialized structures and global variables.
is the memory allocated for uninitialized global and static variables.
There are two main types of RAM available in embedded devices: SRAM (static
(dynamic random access memory). While SRAM
is faster in read/write/access operation, it is also more expensive and usually takes
more physical space. On the other hand, DRAM is generally slower in
ration), but cheaper to
an embedded device, the
following discussion stands. Many of the MCUs used by Arduino boards (e.g.,
Arduino MEGA2560) use
(e.g., 2KB for
ATmega328P and 8KB for ATmega2560), thus special care is required in writing the
M term for both, SRAM and
stack
the problem is caused by insufficient free RAM, and not
by various other possible reasons, such as a defective MCU, problem with peripherals
obvious code bugs. Debugging an Arduino is not really easy since it does
not "beeps" on error, does not show blue screens and also does not trigger popup
windows telling you which is the possible problem. The RAM available in an Arduino
avr-libc).
is the first RAM section and it is used to store program static data, such
is the memory allocated for uninitialized global and static variables.
25. heap is the dynamic memory area, and this is the playground area for malloc (and
alike). The heap can grow (when new allocation is made) or "possibly" decrease in size
(when memory is released, as for example when using free) based on the requirements.
stack is the memory area located at the end of the RAM and it grows towards the heap
area. The stack area is used for function calls, storing values for local variables.
Memory occupied by local variables is reclaimed when the function call finished.
external RAM is only available to some of the MCUs and it means that it is possible to
add RAM in a kind of similar way that we do for a PC. Usually this is expensive (a few
KB of external RAM costs in general more than the MCU) and requires also advanced
hardware and software skills.
free available memory is the area between heap and stack and this is what we need to
measure in order to detect problems caused by not enough RAM resources.When this
area is either too small for the required tasks, or is missing at all (heap meets stack),
our MCU starts to missbehave or to restart itself.
Avoid using dynamic memory allocation
While using dynamic memory allocation is a good solution when programming a
normal PC with multiple hundreds of megabytes, gigabytes or even terabytes of ram, it
is in general a bad idea for embedded devices (such as the Arduino family). The
problem with dynamic memory allocation is that may easily produce memory (heap
area) fragmentation. Memory fragmentation can be seen as small "holes" in the RAM
which can't be reused in many cases.
A few simple rules may help to avoid RAM fragmentation:
Use stack instead of heap whenever possible - stack memory is preferred because the
memory is complete freed up when the function returns, and also the stack memory is
fragmentation free. In general, this means using local variables and avoid using
dynamic memory allocation( i.e., malloc, calloc and realloc calls).
Avoid using global and static data whenever possible - the memory area (.data
variables and .bss variables) occupied by these variables is never freed up for the live
time of the same program.
When using strings is a must, then it is important to keep them as short as possible -
remember, each single char takes one byte of RAM (the entire 2KB RAM memory of
an ATmega328p can be occupied by a string with a length of 2048 chars).
When using arrays, try to keep their length at minimum - if later you really need a
different length, just increase/decrease it the and reprogram your MCU.
26. The static keyword is used to create variables that are visible to only one function.
However unlike local variables that get created and destroyed every time a function is
called, static variables persist beyond the function call, preserving their data between
function calls. Variables declared as static will only be created and initialized the first
time a function is called.
Local Variables are stored on heap.
The automatic memory management uses the stack, and the dynamic memory
allocation uses the heap. Dynamic memory is managed and served with pointers that
point to the newly allocated space of memory in an area which we call the heap.
Example dynamic memory allocation: ptr = (int *) malloc (50)
When this statement is successfully executed, a memory space of 50 bytes is
reserved[19]
27. Future scope
The following features are to be further incorporated to the project:
The project will be provided a remote operation feature by using a GSM module or a
Wi-Fi module such as ESP series to make the system remote operational.
The system will be provided with more than one output (almost 4 using current design,
and many more devices using some slave controllers) to switch multiple devices at
once.
The system may also be incorporated with a camera so that in case of some distress
(gas leak, theft alarm) the user can use mobile phone to get an alert and see through the
camera over internet.