Showing posts with label potentiometer. Show all posts
Showing posts with label potentiometer. Show all posts

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.

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. ...