All posts by Mahendra Raisinghani

Mahendra Raisinghani is co-founder and director of both Shoolin Labs, Jaipur and Lifegraph Biomedical Instrumentation Pvt Ltd at Incubation Center, IIT Patna.

DATA TYPE CONVERSION IN ARDUINO

This tutorial covers data type conversion in arduino. Data type conversion or typecasting means converting a value from one data type to other. For example, convert int to float, string to int etc.

Data type covered in this section are int, float, char, char array, string and const char *.

DATA TYPE CONVERSION IN ARDUINO

INT TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from int to other, see below

  • Convert int to float in Arduino
  • Convert int to char in Arduino
  • Convert int to char array in Arduino
  • Convert int to String in Arduino
data type conversion in arduino
Data type conversion: Int to float, char, char array[] and String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int a = 65;
float d;
char f;
char h[5];
String j;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int a = 65;");
  Serial.println("float d;");
  Serial.println("char f;");
  Serial.println("char h[5];");
  Serial.println("String j;");
  Serial.println();


  /* "INT to OTHER"
    Data type conversion*/
  Serial.println("INT to OTHER Data type conversion");
  delay(delcat);

  d = a;
  Serial.print("integer 65 to float : ");
  delay(delexp);  
  Serial.println(d);

  delay(delsub);
  f = a;
  Serial.print("int 65 to char : ");
  delay(delexp);  
  Serial.println(f);

  delay(delsub);
  itoa(a, h, 10); // int, char[], base
  Serial.print("int 65 to char array : ");
  delay(delexp);  
  Serial.print(h[0]);
  Serial.print(" , ");
  delay(delexp);  
  Serial.println(h[1]);

  delay(delsub);
  j = String(a);
  Serial.print("int 65 to String : ");
  delay(delexp);  
  Serial.println(j);
  Serial.println();

}

void loop() {

}

OUTPUT

data type conversion in arduino

FLOAT TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from float to other, see below

  • Convert float to int in Arduino
  • Convert float to String in Arduino
convert float to
Data type conversion: Float to int and String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int b;
float c = 1.2;
String j;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int b;");
  Serial.println("float c = 1.2;");
  Serial.println("String j;");
  Serial.println();


  /* "FLOAT to OTHER"
    Data type conversion*/
  Serial.println("FLOAT to OTHER Data type conversion");
  delay(delcat);

  b = int(c);
  Serial.print("float 1.2 to int : ");
  delay(delexp);  
  Serial.println(b);

  delay(delsub);
  j = String(c);
  Serial.print("float 1.2 to String : ");
  delay(delexp);  
  Serial.println(j);
  Serial.println();
}

void loop(){
  
}

OUTPUT

data type conversion in arduino

CHAR TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from char to other, see below

  • Convert char to int in Arduino
  • Convert char to float in Arduino
  • Convert char to String in Arduino
char to int
Data type conversion: Char to int, float and String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int b;
float d;
char e = 'a';
String j;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int b;");
  Serial.println("float d;");
  Serial.println("char e = 'a';");
  Serial.println("String j;");
  Serial.println();


  /* "CHAR to OTHER"
    Data type conversion*/
  Serial.println("CHAR to OTHER Data type conversion");
  delay(delcat);

  b = int(e);
  Serial.print("char 'a' to int : ");
  delay(delexp);  
  Serial.println(b);

  delay(delsub);
  d = float(e);
  Serial.print("char 'a' to float : ");
  delay(delexp);  
  Serial.println(d);

  delay(delsub);
  j = String(e);
  Serial.print("char 'a' to String : ");
  delay(delexp);  
  Serial.println(j);
  Serial.println();

}
void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

data type conversion in arduino

CHAR ARRAY TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from char array [] or character array to other, see below

  • Convert char array to int in Arduino
  • Convert char array to String in Arduino
char array to String
Data type conversion: Character array to int and String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int b;
char g[] = {'1', '2', '3', 'D', 'E'};
String j;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int b;");
  Serial.println("char g[] = {'1', '2', '3', 'D', 'E'};");
  Serial.println("String j;");
  Serial.println();


  /* "CHAR ARRAY to OTHER"
    Data type conversion*/
  Serial.println("CHAR ARRAY to OTHER Data type conversion");
  delay(delcat);

  b = atoi(g);
  Serial.print("char array {'1', '2', '3', 'D', 'E'} to int : ");
  delay(delexp);
  Serial.println(b);

  delay(delsub);
  j = String(g);
  Serial.print("char array {'1', '2', '3', 'D', 'E'} to String : ");
  delay(delexp);
  Serial.println(j);
  Serial.println();


}
void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

data type conversion in arduino

STRING TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from String to other, see below

  • Convert String to int in Arduino
  • Convert String to char array [] (character array) in Arduino
  • Convert String to const char * in Arduino
string to const char *
Data type conversion: String to int, character array[] and const char pointer
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int b;
char h[5];
String i = "92AB1";
const char *k;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int b;");
  Serial.println("char h[5];");
  Serial.println("String i = \"92AB1\";");
  Serial.println("const char *k;");
  Serial.println();


  /* "String to OTHER"
    Data type conversion*/
  Serial.println("String to OTHER Data type conversion");
  delay(delcat);

  b = i.toInt();
  Serial.print("String 92AB1 to Int : ");
  delay(delexp);  
  Serial.println(b);

  delay(delsub);
  i.toCharArray(h, 10); // buffer (variable in which to store), max length of buffer can be
  Serial.print("String 92AB1 to Char Array : ");
  delay(delexp);  
  Serial.println(h);

  delay(delsub);
  k = i.c_str();
  Serial.print("String 92AB1 to Const char* : ");
  delay(delexp);  
  Serial.println(k);
  Serial.println();


}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

data type conversion in arduino

CONST CHAR TO STRING CONVERSION

This Arduino code will convert data types from String to other, see below

  • Convert const char * to String in Arduino
const char * to String
Data type conversion: const char * to String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

String j;
const char *l = "HELLO";

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("String j;");
  Serial.println("const char *l = \"HELLO\";");
  Serial.println();


  /* "Const char to OTHER"
    Data type conversion*/
  Serial.println("Const char to OTHER Data type conversion");
  delay(delcat);

  j = String(l);
  Serial.print("Const Char to String : ");
  delay(delexp);  
  Serial.println(j);
  Serial.println();

}

void loop() {

}

const char to string in arduino

 

 

Total Visits: 6247

8X8 LED MATRIX WITH ARDUINO

In this project you will see how to Interface 8×8 led matrix with Arduino or 8×8 dot led matrix with Arduino. In this section we will connect Arduino and led matrix display and send data wirelessly using Bluetooth HC05.

REQUIRED MATERIAL

S.N.COMPONENTQUANTITY
1.Arduino Uno1
2. 8x8 LED matrix module of 41
3.Bluetooth HC051
4. Jumper wires10 (approx)

MAX7219 DOT LED MATRIX 4 in 1 MODULE

8x8 LED matrix

CIRCUIT DIAGRAM

8X8 LED MATRIX WITH ARDUINO
Click to Zoom

CONNECTION TABLE

ARDUINO BLUETOOTH HC05
+5 V VCC (+5 V)
GND GND
0 (RX) TX
1 (TX) RX

 

ARDUINO 8X8 LED MATRIX
+5 V VCC (+5 V)
GND GND
11 DIN
10 CS
13 CLK

WORKING

After uploading arduino code to board, connect the components as shown in the circuit diagram above then you need to pair your Android phone with Bluetooth HC05 (how to pair) using an Android App (download app).

Once you get paired, then write any message in text box and click over Send button to send data wirelessly, which will appear on LED matrix display board.

As you sent data from your phone it will received by bluetooth HC05 and it transfer data bytes to Arduino Serial pins, where arduino is instructed to read Serial data and display it on 8×8 LED LED Matrix board.

ARDUINO PROGRAMMING CODE

To get run the following code you need to include these Arduino Libraries if not already included.  MD_Parola-3.1.1 (at S.N. 8) and MD_MAX72xx-master (at S.N. 9).

I have FC16 hardware type, you may have other hardware type such as PAROLA_HW, GENERIC_HW, ICSTATION_HW or FC16_HW. Change your hardware type if data in led matrix is not appears properly.

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4

// for NODE MCU
//#define CLK_PIN   D5
//#define DATA_PIN  D7
//#define CS_PIN    D8

// for ARDUINO
#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    10

MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
uint8_t scrollSpeed = 35;
textEffect_t scrollEffect = PA_SCROLL_LEFT;
textPosition_t scrollAlign = PA_LEFT;
uint16_t scrollPause = 100;
#define  BUF_SIZE  75
char curMessage[BUF_SIZE] = { "" };
char newMessage[BUF_SIZE] = { "PIJA EDUCATION" };
bool newMessageAvailable = true;

void setup() {
  Serial.begin(9600);
  P.begin();
  P.displayText(curMessage, scrollAlign, scrollSpeed, scrollPause, scrollEffect, scrollEffect);
}

void loop() {
  if (P.displayAnimate()) {
    if (newMessageAvailable) {
      strcpy(curMessage, newMessage);
      newMessageAvailable = false;
    }
    P.displayReset();
  }
  if (Serial.available()>0) {
    String st = Serial.readString();
    Serial.println(st);
    st.toCharArray(newMessage, BUF_SIZE);
    newMessageAvailable = true;
  }
}

OUTPUT

Pair android phone with Bluetooth HC05 using Android App. Then write data in text box and click send and your message will get display on 8×8 dot matrix led display.

MAX7219 8x8 Dot LED MATRIX

 

Total Visits: 871

ROBOT CAR APK

This section tells you, how to use ‘Robot Car App’, designed for Bluetooth control robot car using Arduino and to receive or transmit serial data from Arduino (via Bluetooth HC05 or HC06). Download Robot car apk.

ROBOT CAR APK

Download and Install latest version of Robot car apk. and follow these steps to configure your phone with app.

1. Turn on your android phone Bluetooth and search for the available Bluetooth devices. There you find either HC05 or any other depends on which one you are using.

2. Now enter default password 1234 or 0000 to pair with the Bluetooth HC05 (General method to connect with any other BT device).

Bluetooth HC05 Pairing
Click to Zoom

3. After successfully pairing your phone with HC05, Now open Robo Car App. You might see this screen, Just click on screen other than dialogue box.

Robot car
Click to Zoom

4. Now sets commands like ‘f’ for forward and ‘b’ for backward etc. and click SAVE.

control bluetooth robot car instruction
Click to Zoom

5. Click on “Available BT Devices” and you will see all paired bluetooth devices there, select your BT device you want to connect to. Here I want to connect to HC-06.

robot car apk

6. Now you can click over forward, backward, right and left arrow keys to control your signal. Where forwards sends ‘f’,  backward sends ‘b’, right sends ‘r’ and left sends ‘l’.

robot car apk
Click to zoom

7. Other than forward, backward you can send custom messages as well. Write your message in text box and click SEND. As well as in this text box you will see all Serial print data.

Serial data bluetooth
Click to Zoom

8. Now if you want you can disconnect your phone with bluetooth HC05 by clicking on DISCONNECT button

connect mobile with hc05
Click to zoom

 

ALSO READ: BLUETOOTH CONTROLLED ROBOT CAR USING ARDUINO

Total Visits: 828