In this post, we will be introducing embedded systems using Arduino as our target hardware. What is an embedded system you say? An embedded system is a combination of computer hardware and software designed for a particular purpose or functions among a bigger system. The systems can either be programmable or with fixed functionality.

Why should you start learning embedded system using arduino. Simply answer it is straight forward cheap and very to understand.

Examples of embedded system can be found in devices like,

  • Smart watches
  • Security system
  • Car Alarms
  • Cooking Timer
  • Drones
  • Biometric Verification Systems
  • Digital calculator
  • Wi-fi routers
  • Digital watch
  • Virtual Reality Glass
  • Projectors
  • Digital speedometer
  • ATM’s Machine
  • Digital speedometer

The list of embedded system devices goes on and on check-out this post here but today in this post we will be learning how to control some hardware using Arduino, by the if you are interested in what is Arduino you can read this post

The first thing we will do is to blink an led. Just like is the tradition of software developers to write hello world, blinking an led is the hardwares developers hello world.

How to Blink n LED

Circut Diagram

This pretty straight forward as every newbie could attempt this. But mind the polarity of the led as the wider side is the negative and the smaller counterpart is the positive.

The Code

Copy and paste the code in your ide or just download everything as a whole in one shot but you would need fritzing to utilize it fully.

void setup() {
  // put your setup code here, to run once:
pinMode(13, OUTPUT); // declare digital pin 13 as output
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(13, HIGH);// turn led attatch to pin 13 off
delay(1000);// delay for 1000 milliseconds or 1 second
digitalWrite(13, LOW); // turn led attatch to pin 13 off
delay(1000); // delay for 1000 milliseconds or 1 second
}

How to blink 3 led

We’ve seen how easy it is to control 1 led, now let’s try 3 different led’s

arduino with 3 led’s

The led are connected to digital pin 2,3 & 4 with their ground connected together.

The Code

const int yellow = 2;
const int red = 3;
const int white = 4;

void setup() {
 
pinMode(yellow , OUTPUT);
pinMode(white , OUTPUT);
pinMode(red , OUTPUT);
}

void loop() {

digitalWrite(yellow , HIGH);
delay(1000);
digitalWrite(yellow , LOW);
digitalWrite(red , HIGH);
delay(1000);
digitalWrite(red , LOW);
digitalWrite(white , HIGH);
delay(1000);
digitalWrite(white , LOW);
delay(1000);
}

Notice in the code I used const int, this is to try to tell the Arduino that the assignment is constant. A constant assignment means there is no re-assignment and by given them a name it will be much easier to read the code. Firstly line 1 to 3 I attached the led’s to pin 2 to 4. I declared that those ins will be used as outputs. Lastly, I started my manipulation.

Reading an Input

Enough writing to the digital pin let’s try to read from the digital pin and perform an action at the same time. To do that we are to use a button as our input then flash an led when the button is pressed.

const int button = 2;
const int led = 13;
int state = 0;

void setup() {
  // put your setup code here, to run once:
pinMode(button , INPUT);
pinMode(led , OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
state = digitalRead(button);
if ( state == HIGH )
{
  
digitalWrite(led, HIGH);
  delay(1000);
}
}

Modifying the code a little we could achieve a toggle switch. Where you press it and it comes on you press it again then it comes off.

const int button = 2;
const int led = 13;
int state = 0;
int condition = 0;
void setup() {
  // put your setup code here, to run once:
pinMode(button , INPUT);
pinMode(led , OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
state = digitalRead(button);
if ( state == HIGH && condition == 0)
{
condition = 1;  
digitalWrite(led, HIGH);
  delay(1000);
}
else if (state == HIGH && condition == 1){
  condition = 0
  digitalWrite(led, LOW);
  delay(1000);
}
}

The difference in the code is that; First of all, I added another variable to check whether the led is on or off. Secondly, check to see if the switch has been pressed or not. Thirdly we toggle the led based on the previous two judgement.

Sadly enough as much as i like to program in a high level language, the second code can be achieved with a much compact code in assembly.

The Analog Pin

Most people at the mention of the analog pin would assume that they not as good as their digital counterparts. But in the world of data, analogue is far more precise. Digital pins understand only binary 0’s and 1’s. That is either a high and a low or the presence and absence of voltage. Analog on the hand can give more precise readings as they vary from 0 to 1023. And as such better to use with sensors to collect data. When using analog pins you do not declare it as input or output. We are going to use an analog sensor with an Arduino and blink an led if the moisture is above a certain level.

int state = 0;
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
}

void loop()
{

state = analogRead(A0);
Serial.println(state);
if(state < 600)
{
digitalWrite(13, HIGH);
delay(1000);
}

}

Leave a Reply