Monday, December 21, 2020

Reading Potentiometer value using Arduino - Software creation using Ardino IDE and C Programming Language

 In previous article we have seen the steps in creating the software for the reading Potentiometer value using Simulink for Arduino board. In this article we will look at another way of generating the software and loading to the Arduino board. We will use Arduino IDE and C programming language to achieve the same functionality as the previous article.

If you already doesn't have the Arduino IDE head to the following link and download the IDE.

https://www.arduino.cc/en/software

The following are the functions used in our C Program

  • pinMode
  • digitalWrite
  • delay
  • analogRead
  • map

Functions used in the Potentiometer value reading software for Arduino

For the function help related to pinMode, digitalWrite, delay pleasse visit this article. In this article we are using two new functions and they are analogRead and map functions. Let see the how analogRead and map functions work in Arduino.

analogRead Reads the value from the specified analog pin. This function takes the name of the analog input pin to read from as function argument/parameter. The function call returns the analog value of the reading in the range from 0-1023, this is due to that the analog to digital convertors on the Arduino are the 10 bit ADC's.

map function takes the value and then remaps this value from one range to another range. The function call for map looks like map(analog_value, analog_LSB, analog_MSB, newLow, newHigh), as you can see this function takes the value and then maps the value from one range to another range. It is always useful to know the signal maximum and minimum values beforehand. This function returns the value in the new range based in input value.

Lets dive into the code for reading the potentiometer value and showing the mapped value using the LED's.

Step 1: Set the pin mode in setup function

#define Bit0_LED 3
#define Bit1_LED 4
#define Bit2_LED 5

int pot_value = 0;
int mapped_value = 0;

void setup() {
  // put your setup code here, to run once:
  // Set the pin mode for arduino digital pins.
  pinMode(Bit0_LED, OUTPUT);
  pinMode(Bit1_LED, OUTPUT);
  pinMode(Bit2_LED, OUTPUT);
  
  // Set the initial state for the digital pins.
  digitalWrite(Bit0_LED, LOW);
  digitalWrite(Bit1_LED, LOW);
  digitalWrite(Bit2_LED, LOW);
}

Step 2: Set up the logic in loop function

void loop() 
{
  // put your main code here, to run repeatedly:
  pot_value = analogRead(A2);
  mapped_value = map(pot_value, 0, 1023, 1, 4);
  digitalWrite(Bit0_LED, mapped_value&0x1);
  digitalWrite(Bit1_LED, ((mapped_value&0x2)>>1));
  digitalWrite(Bit2_LED, ((mapped_value&0x4)>>2));
  delay(10);
} 

As you can see in the loop function analogRead function is called by passing the A2 as the parameter, potentiometer is connected to the pin A2 of the arduino. Once the analogRead function is called the value is mapped to lower range i.e 1 to 4 and then the mapped value is used to turn on and off the LED's.

In the loop function to turn on and off the LEDs if else logic is not used but just the hexadecimal bit manipulation is used. You may wounder how this is working. Let consider the mapped value is 1 so for that we need to light only the Bit0_LED and rest of the LED's need to be turned off.

Following is the math behind the hexadecimal trick

mapped_value = 1, multiply this with the hex 1 so we will get 1 as the output and we are passing this to the digitalWrite function with Bit0_LED as the first parameter, so this LED will be turned on. Multiplying mapped_value = 1 with wither 0x2 or 0x4 returns 0 so the other LED's will be turned off the same logic goes for any other number.

Complete program for reading the potentiometer value

#define Bit0_LED 3
#define Bit1_LED 4
#define Bit2_LED 5

int pot_value = 0;
int mapped_value = 0;

void setup() {
  // put your setup code here, to run once:
  // Set the pin mode for arduino digital pins.
  pinMode(Bit0_LED, OUTPUT);
  pinMode(Bit1_LED, OUTPUT);
  pinMode(Bit2_LED, OUTPUT);
  
  // Set the initial state for the digital pins.
  digitalWrite(Bit0_LED, LOW);
  digitalWrite(Bit1_LED, LOW);
  digitalWrite(Bit2_LED, LOW);
}

void loop() {
  // put your main code here, to run repeatedly:
  pot_value = analogRead(A2);
  mapped_value = map(pot_value, 0, 1023, 1, 4);
  digitalWrite(Bit0_LED, mapped_value&0x1);
  digitalWrite(Bit1_LED, ((mapped_value&0x2)>>1));
  digitalWrite(Bit2_LED, ((mapped_value&0x4)>>2));
  delay(10);
}

Sunday, December 20, 2020

Reading Potentiometer value using Arduino - Software Loading into Arduino and Demo

 In earlier article we have seen the software creation using pure Simulink with out using any Stateflow for reading and displaying the potentiometer value. In this article we will explore the software deployment, circuit diagram and demo.

Schematic for reading potentiometer value using Arduino

 Potentiometer wiper is connected to the Arduino analog pin 2, 5V power and ground is supplied from Arduino. Three LED's are connected to the Arduino digital pin 3,4 and 5 to display the potentiometer reading in binary format. When the processed analog value is 1 only first LED will be turned on and rest all LED's are turned off. So these three LED's represent the three bits of the binary number.

Software deployment to Arduino from Simulink 

Following video presents the steps required to prepare the Simulink model for code generation for the Arduino board. The steps for the code generation are same as the steps explained in this article


 Demo for Potentiometer value reading using Arduino


As you can see in the demo video the potentiometer value is read by the Arduino using the analog pin 2 and then process it and displayed the mapped value using three white LED's.

I hope you enjoyed the article and we will catch up in our next article where we will discuss the traffic lights simulation for four junction signals.

Saturday, December 19, 2020

Reading Potentiometer value using Arduino - Software creation using Simulink and MATLAB

 In our previous article we have seen to control three LED's together using the Arduino and the software is developed using Stateflow as a state machine. In this article we will look at generating the software from the Simulink blocks to read the potentiometer value and showing that as a binary value using three LED's.

Required components

  • Three LEDs
  • Arduino
  • Connecting Wires
  • Three Resistors
  • Matlab or Arduino IDE
  • Potentiometer

The idea is to read the potentiometer reading using Arduino and converting the read value from 0 to 1023 scale to 1 to 4 scale. So all the potentiometer values from 0 to 256 are represented using 1 and 257 to 512 are represented using 2 and so on.

Software creation for Arduino using Simulink

In our earlier posts we have seen using the stateflow, in this article we see the Simulink in action all of the software is developed using Simulink graphically. At the end the Simulink model is deployed to the Arduino using Simulink embedded coder.

Following video explains the software creation to read and process the analog value using Arduino and Simulink.


In the next article we will explore the code generation and deployment to the hardware along with demo.

LED's Sequential Lighting Using Arduino - Software creation using Ardino IDE and C Programming Language

 In our earlier post we have seen creating the software for Arduino using MATLAB and Stateflow. In this article we will look at another way of generating the software and loading to the Arduino board. We will use Arduino IDE and C programming language to achieve the same functionality as the earlier post.

If you already doesn't have the Arduino IDE head to the following link and download the IDE.

https://www.arduino.cc/en/software

The following are the functions used in our C Program

  • pinMode
  • digitalWrite
  • delay

Functions used in the LED Blinking software for Arduino

pinMode function helps to set the Arduino pins either in write or read mode. This function takes two inputs. First one is the Arduino pin number and the second input is the mode. When ever we need to send output from the Arduino we will use the OUTPUT as the mode and when we need to read from the Arduino pin we will use INPUT as the mode.

digitalWrite function helps to write the voltage value to the digital pin of the Arduino. This function takes two inputs. First one is the Arduino pin number and the second input is the voltage value. When ever we need to activate the digital output we use HIGH otherwise we will use LOW to set the Arduino pin to 0 volts.

delay function takes the number of milliseconds as the input and stops the program execution for the supplied milliseconds. For example if we need to wait for 2 seconds at the same state we will call delay function with 2000 as input.

Let employ the divide and conquer strategy and divide our program into smaller steps. Following are the steps we will follow to achieve the end goal. 

Step 1: Set the pin mode and Initial state in setup function

#define Red_LED 3
#define Green_LED 4
#define Yellow_LED 5

void setup() 
{
  // put your setup code here, to run once:
  // Set the pin mode for arduino digital pins.
  pinMode(Red_LED, OUTPUT);
  pinMode(Green_LED, OUTPUT);
  pinMode(Yellow_LED, OUTPUT);
  
  // Set the initial state for the digital pins.
  digitalWrite(Red_LED, LOW);
  digitalWrite(Green_LED, LOW);
  digitalWrite(Yellow_LED, LOW);
} 

Step 2: Define loop function

void loop() 
{
  // put your main code here, to run repeatedly:
  // Turn on Green LED for two seconds
  digitalWrite(Green_LED, HIGH);
  delay(2000);
  digitalWrite(Green_LED, LOW);

  // Turn on Yellow LED for one seconds
  digitalWrite(Yellow_LED, HIGH);
  delay(1000);
  digitalWrite(Yellow_LED, LOW);

  // Turn on Red LED for two seconds
  digitalWrite(Red_LED, HIGH);
  delay(2000);
  digitalWrite(Red_LED, LOW);
} 

We have set the Arduino Digital pin connected to the Green LED to high and called the delay function with 2000. The effect is Arduino sets the digital pin 4 to high and waits for two seconds to execute next code line so the Green LED turns on and stays at the state for two seconds and then the next code line is executed that sets the digital pin 4 to low so the Green LED turns off.

The same concept is applied for digital pin 5 and 6 which are connected to the Yellow LED and Red LED respectively. So the complete code forms a state machine which turns on and off each LED one after another.

Complete Code for the LED's Sequential Lighting Using Arduino

#define Red_LED 3
#define Green_LED 4
#define Yellow_LED 5

void setup() 
{
  // put your setup code here, to run once:
  // Set the pin mode for arduino digital pins.
  pinMode(Red_LED, OUTPUT);
  pinMode(Green_LED, OUTPUT);
  pinMode(Yellow_LED, OUTPUT);
  
  // Set the initial state for the digital pins.
  digitalWrite(Red_LED, LOW);
  digitalWrite(Green_LED, LOW);
  digitalWrite(Yellow_LED, LOW);
}

void loop() 
{
  // put your main code here, to run repeatedly:
  // Turn on Green LED for two seconds
  digitalWrite(Green_LED, HIGH);
  delay(2000);
  digitalWrite(Green_LED, LOW);

  // Turn on Yellow LED for one seconds
  digitalWrite(Yellow_LED, HIGH);
  delay(1000);
  digitalWrite(Yellow_LED, LOW);

  // Turn on Red LED for two seconds
  digitalWrite(Red_LED, HIGH);
  delay(2000);
  digitalWrite(Red_LED, LOW);
} 

I hope you enjoyed the LED's sequential lighting demo in this article and in our next article we will explore the potentiometer interfacing with Arduino.

LEDs Sequential lighting - Software Loading into Arduino and Demo

 In the previous post we have seen the developing of the software for the LED's sequential lighting using Stateflow. In this article we will explore the circuit diagram for the LED's sequential lighting and software deployment parts.

Schematic for Arduino and LED's Sequential Lighting

Following is the circuit diagram for the LED's sequential lighting using Arduino.

Arduino digital pin 3 is connected to the Red LED anode, digital pin 4 is connected to the Green LED anode and digital pin 5 is connected to the Yellow LED's anode. All the LED's cathodes are connected to the ground via a resistor. This resistor is used as current limiting resistor to limit the current draw from Arduino and saving the micro-controller from frying up.

Software deployment to Arduino from Simulink

Following video presents the steps required to prepare the Simulink model for code generation for the Arduino board. The steps for the code generation are same as the steps explained in this article

Demo for the LED's Sequential Lighting using Arduino

The following video shows the demo of the article on the Arduino Mega hardware, you can see the working of software created in earlier article


I hope you enjoyed the LED's sequential lighting demo in this article and in our next article we will explore the potentiometer interfacing with Arduino.

You can checkout how the LED's Sequential Lighting can be implemented using C programming language and Arduino IDE in this article.


LEDs Sequential lighting - Software creation using Simulink and Stateflow for Arduino

 In the previous article we have seen how to blink the single LED using Arduino and the software is developed using Simulink and Stateflow charts. In this article we learn about how to light three different LEDs in sequential manner. 

Required components

  • Three LEDs (Red, Green, Yellow)
  • Arduino
  • Connecting Wires
  • Three Resistors
  • Matlab or Arduino IDE

The idea is to turn only one LED at any time. The sequence is Green LED turns on first and then Yellow LED and finally Red LED and this sequence continues. Green LED will be on for 2 seconds, Yellow LED for 1 second and Red LED for 2 seconds.

Software creation for Arduino using Stateflow

All this is implemented as a stateflow and temporal logic as a transition condition. In earlier article we just used entry condition in the state and this article introduces one more condition called exit condition.

The details of creating the stateflow chart for sequential control of three LEDs is explained in the following video.


 In our next article we will deploy the code generated from the model to the Arduino board and test our logic on the actual hardware.

Monday, December 14, 2020

LED Blikining using Arduino - Software creation using Ardino IDE and C Programming Language

 In our earlier posts we have seen the steps to create the software using Simulink and Stateflow for the LED Blinking program and deployed the software to the Arduino Board. In this article we will look at another way of generating the software and loading to the Arduino board. We will use Arduino IDE and C programming language to achieve the same functionality as the earlier post.

If you already doesn't have the Arduino IDE head to the following link and download the IDE.

https://www.arduino.cc/en/software

The following are the functions used in our C Program

  • pinMode
  • digitalWrite
  • delay

Functions used in the LED Blinking software for Arduino

pinMode function helps to set the Arduino pins either in write or read mode. This function takes two inputs. First one is the Arduino pin number and the second input is the mode. When ever we need to send output from the Arduino we will use the OUTPUT as the mode and when we need to read from the Arduino pin we will use INPUT as the mode.

digitalWrite function helps to write the voltage value to the digital pin of the Arduino. This function takes two inputs. First one is the Arduino pin number and the second input is the voltage value. When ever we need to activate the digital output we use HIGH otherwise we will use LOW to set the Arduino pin to 0 volts.

delay function takes the number of milliseconds as the input and stops the program execution for the supplied milliseconds. For example if we need to wait for 2 seconds at the same state we will call delay function with 2000 as input.

Let employ the divide and conquer strategy and divide our program into smaller steps. Following are the steps we will follow to achieve the end goal. 

Step 1: Set the pin mode in setup function

#define LED_PIN 9

void setup() 
{
  // put your setup code here, to run once:
  //Set the Arduino Digital Pin 9 Mode
  pinMode(LED_PIN, OUTPUT);
}

Step 2: Define loop function

void loop() 
{
  // put your main code here, to run repeatedly:
  // Toggle the LED state
  digitalWrite(LED_PIN, HIGH);
  delay(500);
  digitalWrite(LED_PIN, LOW);
  delay(500);
}

Set the value of the digital pin to HIGH and LOW repeatedly for every 0.5 seconds. By default Arduino sketch comes up with loop function so we don't have to worry about creating our own loop. What ever coded in the loop function will be called in a cycle.

Complete Code for the LED Blinking Using Arduino and Arduino IDE

Now look at the complete code which forms our Arduino board software to Blink the LED.

#define LED_PIN 9

void setup() 
{
  // put your setup code here, to run once:
  //Set the Arduino Digital Pin 9 Mode
  pinMode(LED_PIN, OUTPUT);
}

void loop() 
{
  // put your main code here, to run repeatedly:
  // Toggle the LED state
  digitalWrite(LED_PIN, HIGH);
  delay(500);
  digitalWrite(LED_PIN, LOW);
  delay(500);
}

Sunday, December 13, 2020

LED Blinking using Arduino and Matlab - Software Loading into Arduino and Demo

 In previous post we have seen the steps to create the Simulink model for the LED Blinking. In this post we will learn about configuring and preparing the model for code generation and deployment to the Arduino board.

Steps for preparing the Simulink model for deployment to Arduino

 The following steps help you to prepare the model for the deployment to the hardware in our case Arduino board.

  • Open the model configuration parameters from the Simulation window or select the little triangle icon beside gear icon in the tool bar and select "Model Configuration Parameters"
  • Select the "Hardware Implementation" option from the right options pane
  • Next choose the "Hardware board" from the drop down list available in our case we will select the Arduino. It can be either Arduino UNO or Arduino Mega 2560 based on your board.
  • Click save for the model configuration parameters.
  • Select the C/C++ Code from the Code menu and then click on Deploy to Hardware option or you can simply press Ctrl+B to deploy the software to the hardware board.
  • After successful code generation the code generation report will be opened automatically and you can browse the code from the simple HTML window popped up. 
  • MATLAB automatically generated the hex file for the Arduino board and deploys it using the connected USB cable.

All the above listed steps are explained in the following video.


 Schematic

 The Arduino schematic is developed using Fritzing software and is as follows, Arduino digital pin 9 is connected to the anode of the LED and the cathode of the LED is connected to the ground via current limiting resistor.

LED Blinking Demo using Arduino

You can check the following video for the LED Blinking demo, as you can see in the video the LED blinks with 0.5sec cycle.


 I hope you enjoyed the LED Blinking project using Arduino, Simulink and Stateflow.

Friday, December 11, 2020

LED Blinking using Arduino and Matlab - Software Creation using Stateflow and Simulink

 In this article we will work on LED blinking program using Arduino. The software required for the LED blinking is developed using MATLAB/Simulink and Stateflow charts. 

The required components

  • One LED
  • Arduino
  • Connecting Wires
  • Resistor
  • Matlab or Arduino IDE

In this article we will use the MATLAB, Simulink and Stateflow to create the required software to blink the LED. In next article we will explore the same using Arduino IDE and C programming language.

Matlab has support for Arduino development and we can use the blocks from  "Simulink Support Package for Arduino". You can follow the below Matlab support article to get the blocks for Arduino

https://in.mathworks.com/hardware-support/arduino-simulink.html

Steps for creating the Simulink model for LED blinking

  • Create an empty model
  • Open the Simulink Library browser
  • Drag and Drop the Digital Output block from "Simulink Support Package for Arduino Hardware"
  • Drag and Drop the Stateflow chart to the model
  • The following video shows the step by step process


     
  •  Now we have seen how to create the software using Simulink and Stateflow and the next step is preparing the Simulink model for the code generation.
  • In our next post we will see how to prepare our LED blinking model for the embedded code generation and how to load the generated executable into Arduino Board.

Stateflow

  • Stateflow is MATLAB is used to create the state machines.
  • In above video we have seen how to use state and entry criteria to control the LED on and off.
  • entry condition in the Stateflow is used to set the state of the signals at start of the state and they will stay remained at the assigned values.

Reading Potentiometer value using Arduino - Software creation using Ardino IDE and C Programming Language

 In previous article we have seen the steps in creating the software for the reading Potentiometer value using Simulink for Arduino board. ...