Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

QUESTION ONE (20 marks) Compulsory

a) Explain how an embedded system differs from a general-purpose computer.


[2 marks]

b) Determining the minimum pulse duration to be detected sometimes requires calculation.


Consider a sensor in a street used to count the number of cars that pass over the sensor. If the
sensor detects just a single point above the sensor, the shortest car is 6 feet long, and the
fastest speed to be considered is 200 mph, calculate the minimum pulse duration for a single
car passing over the sensor. [6 marks]
To calculate the minimum pulse duration for a single car passing over the sensor, we
need to determine the time it takes for the car to pass the sensor completely. This time
can be calculated using the formula:

Time
=
Distance
Speed
Time=
Speed
Distance

Given:

Length of the shortest car = 6 feet


Fastest speed to be considered = 200 mph
First, we need to convert the length of the car from feet to miles (since speed is given in
mph):

Length of the car in miles


=
6
5280
miles

0.00113636
miles
Length of the car in miles=
5280
6

miles≈0.00113636 miles
Now, we can use the formula to calculate the time:

Time
=
0.00113636
miles
200
mph

5.6818
×
1
0

6
hours
Time=
200 mph
0.00113636 miles

≈5.6818×10
−6
hours
To convert this time to seconds, we multiply by 3600 (since 1 hour = 3600 seconds):

Time

5.6818
×
1
0

6
hours
×
3600
seconds/hour

0.0204548
seconds
Time≈5.6818×10
−6
hours×3600 seconds/hour≈0.0204548 seconds
So, the minimum pulse duration for a single car passing over the sensor is approximately
0.0204548
0.0204548 seconds, or about
20.4548
20.4548 milliseconds.

c) Name and explain Four characteristics of embedded systems [4 marks]


Real-time Operation:

​ Embedded systems often require real-time operation, meaning they must


respond to inputs or events within a specific time frame. This ensures timely
and deterministic behavior critical for applications like automotive control
systems, industrial automation, or medical devices.
Low Power Consumption:

​ Many embedded systems operate on battery power or have strict power


constraints. Therefore, they are designed to optimize power consumption to
extend battery life or reduce energy usage, making them suitable for portable
devices or systems requiring energy efficiency.
Dedicated Functionality:

​ Embedded systems are typically designed for specific dedicated tasks or


functions. Unlike general-purpose computers, which are versatile and can run a
wide range of applications, embedded systems are tailored to perform specific
functions efficiently. Examples include microcontrollers in washing machines,
microwave ovens, or digital cameras.
Limited Resources:

​ Embedded systems often have limited resources such as memory, processing


power, and storage compared to general-purpose computers. This limitation
necessitates optimization of hardware and software to achieve desired
functionality within the constraints. Techniques like code optimization, memory
management, and resource sharing are commonly employed in embedded
system design to maximize efficiency despite limited resources.

d) Discuss the purpose of the setup() and loop() functions in an Arduino sketch and explain how
they are used. Give an example. [4 marks]
setup(): This function is called once when the sketch starts or whenever the Arduino
board is reset. It’s used to initialize variables, input and output pin modes, start using
libraries, etc. The setup() function will only run once, after each power-up or reset of the
Arduino board.

loop(): After setup() runs, the loop() function runs indefinitely over the main program and
controls the board’s behavior based on the input/output of the pins. All of your main
program code goes inside the loop() function.

e) An Arduino board can be used to read data from a temperature sensor, such as a thermistor,
and display the readings on the Serial Monitor using the analogRead() and Serial.print()
functions. The analogRead() function is used to read the voltage level at an analog input pin,
and the value can be converted to a temperature reading using an appropriate equation. The
Serial.print() function is used to send the temperature data to the Serial Monitor for display.
Using the above explanation, write an Arduino code that reads data from a temperature
sensor and displays the reading on a Serial Monitor. [4 marks]

// Define the analog input pin


const int tempPin = A0;

void setup() {
// Start the serial communication
Serial.begin(9600);
}

void loop() {
// Read the voltage level from the temperature sensor
int sensorValue = analogRead(tempPin);
// Convert the sensor reading to temperature (this depends on the specific sensor used)
// Here we assume a linear relationship, which might not be accurate
float voltage = sensorValue * (5.0 / 1023.0); // Convert to voltage
float temperature = (voltage - 0.5) * 100.0; // Convert to temperature in Celsius

// Print the temperature to the Serial Monitor


Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");

// Wait for a second before taking the next reading


delay(1000);
}

QUESTION TW0 (15 marks)


a) An Arduino board can be used to control a LED based on the readings from a light
sensor, such as a photoresistor, by reading the sensor data and using an if statement to
turn the LED on or off based on the sensor value. The analogRead() function is used to
read the sensor data, and the digitalWrite() function is used to set the state of the LED.
Write an Arduino code that can be used to control an LED based on the readings from a
light sensor. [6 marks]
// Define the analog input pin for the light sensor
const int lightSensorPin = A0;

// Define the digital output pin for the LED


const int ledPin = 13;

void setup() {
// Set the LED pin as output
pinMode(ledPin, OUTPUT);
}

void loop() {
// Read the light level from the light sensor
int lightLevel = analogRead(lightSensorPin);

// If the light level is less than 500, turn the LED on


if (lightLevel < 500) {
digitalWrite(ledPin, HIGH);
} else {
// Otherwise, turn the LED off
digitalWrite(ledPin, LOW);
}

// Wait for a moment before taking the next reading


delay(100);
}

b) Implement a synchSM in C for the following toggle system, wherein a first button press
on A0 sets B0 to 1, a second sets B0 to 0, a third to 1, etc. Assume the button may bounce
for up to 20 ms. Assume the maximum latency between a press and toggle should be 50
ms. Assume button presses last at least 250 ms and are separated by at least 500 ms.
Choose a good period for the synchSM. Also describe why a much smaller period or a
much larger period would be bad.
Here are the steps to design the toggle system:
Step 1: Define the states of the synchSM
In this system, we need to keep track of the current state of the toggle. We can use a state
variable 'state' to represent the current state. Since the system has only two possible
states, we can use a simple binary state encoding: 0 for the 'off' state and 1 for the 'on'
state.
Step 2: Define the inputs and outputs of the synchSM
The input to the synchSM is the button press on A0. The output is the value of B0, which
needs to be toggled based on the state of the system.
Step 3: Determine the transition conditions
When a button press is detected, the synchSM should transition to the next state and
toggle the output B0. We also need to ensure that the system meets the requirements of
the problem statement:
• Button press may bounce for up to 20 ms: We can use a debouncing circuit or
software algorithm to filter out any bouncing.
• Maximum latency between a press and toggle should be 50 ms: We can use a
timer to ensure that the toggle happens within 50 ms of a button press.
• Button presses should last at least 250 ms and be separated by at least 500 ms: We
can use a timer to ensure that a button press lasts at least 250 ms and that there is
at least 500 ms between successive button presses.
Step 4: Implement the synchSM in C [9 marks]
#include <avr/io.h>
#include <avr/interrupt.h>

enum States {Start, Init, Wait, Pressed, Released} state;

void Tick() {
static unsigned char button;
static unsigned char led;
button = ~PINA & 0x01; // Assuming the button is connected to PINA0

switch(state) { // Transitions
case Start:
state = Init;
break;
case Init:
state = Wait;
break;
case Wait:
if (button) {
state = Pressed;
}
break;
case Pressed:
if (!button) {
state = Released;
}
break;
case Released:
if (button) {
state = Pressed;
} else {
state = Wait;
}
break;
default:
state = Start;
break;
}

switch(state) { // Actions
case Start:
break;
case Init:
led = 0;
break;
case Wait:
break;
case Pressed:
break;
case Released:
led = !led; // Toggle LED
break;
default:
break;
}

PORTB = led; // Assuming the LED is connected to PORTB0


}

int main(void) {
DDRA = 0x00; PORTA = 0xFF; // Configure port A's 8 pins as inputs
DDRB = 0xFF; PORTB = 0x00; // Configure port B's 8 pins as outputs

TimerSet(50); // Set Timer period to 50 ms


TimerOn(); // Turn on Timer

state = Start;

while(1) {
Tick();
while (!TimerFlag); // Wait for timer period
TimerFlag = 0; // Lower flag raised by timer
}
}

This code uses a timer with a period of 50 ms. This period is chosen because it is the maximum
latency between a button press and a toggle. If the period was much smaller, the system would be
checking the button state more frequently than necessary, which could waste CPU cycles. If the
period was much larger, the system could miss button presses or exceed the maximum allowed
latency of 50 ms. Therefore, a period of 50 ms is a good choice for this system. Please note that
this code assumes the use of the provided TimerISR library for handling the timer interrupts. The
button is connected to PINA0 and the LED is connected to PORTB0. If your connections are
different, please adjust the pin numbers in the code accordingly. Also, this code does not handle
button debouncing. If you need to handle button debouncing, you can add a debouncing state or
use a debouncing library. Please note that the actual conversion from the sensor reading to the
temperature depends on the specific sensor and its characteristics. You might need to replace the
conversion formula with the one provided by the sensor manufacturer. Also, don’t forget to select
the correct baud rate (9600 in this case) in your Serial

QESTION THREE (15 marks)


a) Explain what a state machine and how it is used in embedded systems. [5 marks]
A state machine, also known as a finite state machine (FSM), is a mathematical model of
computation. It is an abstract concept where the machine can have different states, but at
a given time, it can be in only one state. It changes from one state to another state in
response to some external inputs. The change from one state to another is called a
transition.

State machines are used extensively in designing embedded systems for the following
reasons:

Predictability: State machines are deterministic. Given the current state and input, the
next state and output can be precisely defined. This predictability is crucial in real-time
systems where a specific output is expected in response to a given input.

Simplicity: State machines simplify the design of complex systems by breaking them
down into a number of individual states, which can be easily understood and managed.

Debugging and Testing: It’s easier to test and debug state machines because you can
isolate issues to specific states or transitions.

Power Management: State machines are excellent for power management in embedded
systems. You can design states that consume less power when the system is idle.

Control Systems: State machines are commonly used in control systems, where the
system needs to react to real-time events.

Here’s a simple example of a state machine in an embedded system: a traffic light


controller. It has three states (Red, Yellow, Green). The transitions are based on timers
(after a certain time, switch to the next state) or sensor inputs (e.g., no cars detected for a
certain time, switch to Red to allow cross traffic).

b) Explain how state machines be used to control the behavior of an embedded system
with multiple inputs and outputs. Draw a simple illustration [5 marks]
State machines can effectively control the behavior of an embedded system with multiple inputs and
outputs. Each state in the state machine represents a specific behavior or condition of the system,
and the transitions between states are triggered by the inputs. The outputs of the system are
typically determined by its current state.
For instance, consider a simple home security system with two inputs (a door sensor and a window
sensor) and two outputs (an alarm and a notification to your phone). Here’s a simple state diagram:
+----------------+ Door/Window +--------------+
| | Sensor Triggered | |
| System Idle | ------------------> | Alarm Active |
| | <------------------ | |
+----------------+ Reset +--------------+

In this system, there are two states: System Idle and Alarm Active. The system starts in the
System Idle state. If either the door or window sensor is triggered, the system transitions to the
Alarm Active state, activating the alarm and sending a notification to your phone. When the
system is reset (perhaps after you’ve checked that it’s a false alarm), it transitions back to the
System Idle state.
This is a simple example, but real-world embedded systems can have many more states and much
more complex behavior. State machines provide a structured way to design and understand these
systems. They make it clear what the system should do in response to each possible input, making
the system’s behavior predictable and easier to test and debug. They’re a powerful tool for designing
reliable embedded systems.

c) State machines can be used to design a finite state machine (FSM) for a simple traffic
light controller system by defining the states that represent the different light sequences,
and transitions between the states that are triggered by time or other events. In the case
of a traffic light controller, the states could represent the red, yellow, and green lights,
and the transitions could be triggered by a timer or a sensor that detects the presence of
vehicles. A state machine diagram can be used to visualize the states and transitions,
and the logic for the state machine can be implemented in code to control the behavior of
the system. Draw a FSM for a simple traffic light controller. [5 marks]

+---------+ Timer +---------+ Timer +---------+


| | ------------> | | ------------> | |
| Red | | Yellow | | Green |
| | <------------ | | <------------ | |
+---------+ Timer +---------+ Timer +---------+

In this FSM:
● The states represent the three lights: Red, Yellow, and Green.
● The transitions between states are triggered by a timer.
● The system starts in the Red state. When the timer expires, it transitions to the Green state.
● From the Green state, when the timer expires, it transitions to the Yellow state.
● From the Yellow state, when the timer expires, it transitions back to the Red state.
This is a simple cyclic FSM where the system continuously cycles through the Red, Green, and
Yellow states. The duration of each state could be adjusted based on specific traffic conditions or
regulations. For example, the Green state might last longer during peak traffic hours, and the Red
state might last longer during off-peak hours.

QUESTION FOUR (15 marks)


a) Write a program for an embedded system using C program. Define a constant N to
represent the number of temperature conversions to be performed. Then, define an array
celsius of size N to store the temperature values in Celsius, and an array fahrenheit of
size N to store the converted temperature values in Fahrenheit.
Convert the Celsius to Fahrenheit using the formula fahrenheit = (celsius * 9.0 / 5.0) +
32.0. Print the temperature conversions by printing the values in the celsius and
fahrenheit arrays. [6 marks]

#include <stdio.h>

#define N 5 // Define the number of temperature conversions

int main() {
double celsius[N] = {0.0, 10.0, 20.0, 30.0, 40.0}; // Celsius temperatures
double fahrenheit[N]; // Fahrenheit temperatures

// Convert Celsius to Fahrenheit


for (int i = 0; i < N; i++) {
fahrenheit[i] = (celsius[i] * 9.0 / 5.0) + 32.0;
}

// Print the temperature conversions


printf("Celsius\tFahrenheit\n");
for (int i = 0; i < N; i++) {
printf("%.2f\t%.2f\n", celsius[i], fahrenheit[i]);
}

return 0;
}

In this program, we first define a constant N to represent the number of temperature conversions to
be performed. We then define an array celsius of size N to store the temperature values in Celsius,
and an array fahrenheit of size N to store the converted temperature values in Fahrenheit.
We then use a for loop to convert each Celsius temperature to Fahrenheit using the formula
fahrenheit = (celsius * 9.0 / 5.0) + 32.0.
Finally, we print the temperature conversions by printing the values in the celsius and
fahrenheit arrays. The printf function is used to print the temperatures with two decimal
places. The temperatures are printed in a tabular format with the Celsius temperatures in the first
column and the Fahrenheit temperatures in the second column. The \t escape sequence is used to
insert a tab between the Celsius and Fahrenheit temperatures, and the \n escape sequence is used
to insert a new line after each row. The return 0; statement indicates that the program has
finished executing successfully.

b) Digital signal processing (DSP) is a crucial aspect of embedded systems design,


especially when sensors and actuators are involved. DSP enables real-time processing
and analysis of signals from sensors and helps to make more informed decisions based
on the sensor data. This leads to improved system performance, accuracy, and reliability.
Name and explain three advantages and three disadvantages of using DSP in embedded
system. [9 marks]

Digital Signal Processing (DSP) indeed plays a vital role in embedded systems, particularly when
dealing with real-time data from sensors and actuators. Here are some advantages and
disadvantages of using DSP in embedded systems:
Advantages:
1. Real-Time Signal Processing: DSP allows for real-time processing of sensor data. This is
crucial in many embedded systems where timely response to sensor data is required.
2. Improved Accuracy: DSP techniques can improve the accuracy of sensor data by filtering out
noise and other unwanted signals. This leads to more reliable and accurate system
performance.
3. Efficient Data Handling: DSP can efficiently handle large amounts of data from sensors. It
can compress, filter, and transform the data in ways that make it easier to store and analyze.
Disadvantages:
1. Complexity: Implementing DSP algorithms can be complex and requires a good
understanding of mathematics and signal processing techniques. This can increase the
development time and cost.
2. Processing Requirements: DSP algorithms can be computationally intensive, which might
not be suitable for low-power or resource-constrained embedded systems.
3. Design Challenges: The design of DSP systems can be challenging due to the need to
balance between performance, power consumption, cost, and other factors.
Remember, the decision to use DSP in an embedded system depends on the specific requirements
of the system and the trade-offs between these advantages and disadvantages. It’s always
important to consider the system’s requirements and constraints when choosing the appropriate
technologies and techniques.

You might also like