Showing posts with label Arduino Tutorial. Show all posts
Showing posts with label Arduino Tutorial. Show all posts

Tuesday, April 19, 2016

Interfacing a rain sensor to the Arduino

Prepared for the rainy season? No ? How about a little help with the Arduino? Use a rain sensor like this photo below, so you can close a window when you're not at home or drive a pump to store rainwater. or operate wipers of car windshield.
Rain Sensor interfacing with Arduino
The rain sensor for Arduino is a component with two parts: the sensor plate formed by several tracks resistant to oxidation, which will detect the liquid level is reached the plate and the module with LM393 comparator chip, which is responsible for reading the information from this sensor and send the data by pin A0 (analog) or D0 (digital - values ​​0 and 1) . This module also has a potentiometer for sensitivity adjustment, a red lED for indicating oN , and green lED indicates that data transmission:

I quoted at the beginning of the post, you can use a motor connected to the digital output to open (no rain - level HIGH) or close (with rain - level LOW) a window when rain is detected, and the analog output you can open a valve or trigger a pump depending on the intensity of the rain, reading the values ​​from 0 to 1023 provided by this port. Test your module with only 3 leds and 3 resistors, riding the circuit below.The module can be supplied with voltages of 3.3 volts to 5:

Rain Sensor interfacing with arduino
The test program reads the sensor information and illuminates the LEDs according to the liquid level detected by testing the values ​​read in a series of commands IF (IF), which can be added or modified in accordance with the level of accuracy desired.



// Program: Rain Sensor Test 
// Author: blog.circuits4you.com

int pino_d = 2 ; // pin connected to D0 sensor 
int pino_a = A5; // pin connected to A0 sensor 
int val_d = 0 ; // Store the value read from the digital pin 
int val_a = 0 ; // Store the value read from analog pin

// Ports connected to the LED 
int pin_led_high = 5 ;
 int pin_led_medium = 6 ;
 int pin_led_low = 7 ;

void setup ()
{
  // Set the pins on the sensor as input
  pinMode (pino_d, INPUT);
  pinMode (pino_a, INPUT);

  // Set the pins of LEDs as output
  pinMode (pin_led_high, OUTPUT);
  pinMode (pin_led_medium, OUTPUT);
  pinMode (pin_led_low, OUTPUT);
  Serial.begin ( 9600 );
}

void loop ()
{
  // Le arnazena and the value of the digital pin
  val_d = digitalRead (pino_d);
  // Le and stores the value of the analog pin
  val_a = analogRead (pino_a);
  // Send the information to the serial monitor 
  Serial.print ( "Digital Value" );
  Serial.print (val_d);
  Serial.print ( "- Value Analog" );
  Serial.println (val_a);
 
  // Turns on the LED according to the intensity 
  if (val_a> 900 && val_a < 1024 )
  {
    // Lights LED green - low intensity
    digitalWrite (pin_led_high, HIGH);
    digitalWrite (pin_led_medium, LOW);
    digitalWrite (pin_led_low, LOW);
  }
  if (val_a> 400 && val_a < 900 )
  {
    // Lights LED yellow - moderate
    digitalWrite (pin_led_high, LOW);
    digitalWrite (pin_led_medium, HIGH);
    digitalWrite (pin_led_low, LOW);
  }
  if (val_a> 0 && val_a < 400 )
  {
    // Turns on Red LED - High intensity
    digitalWrite (pin_led_high, LOW);
    digitalWrite (pin_led_medium, LOW);
    digitalWrite (pin_led_low, HIGH);
  }
  delay ( 1000 );
}

If you want to test only the sensor, without mounting the entire circuit, the values ​​are also displayed in the serial monitor.

Color recognition sensor TCS230 / TCS3200 with Arduino

Today's tutorial talks about how to use the Arduino to mount a color recognition system using the TCS230 / TCS3200 module .


Color Sensor Module
This module uses the TCS230 sensor, which is composed of 64 photo diodes. These 64 photodiodes, has 16 filters for red, 16 for green, 16 for blue color and 16 has no filter at all.

Distributed evenly over the sensor, these sensors capture light by filtering out colors and generate the output a square wave signal with information about the intensity of the colors red (R = Red) , green (G = Green) andBlue (B = Blue) .

As the module shown above, the TCS230 sensor is usually mounted together with four white LEDs for lighting, and eight pins for connection.

The module accepts power from 3 to 5 volts and are used 5 - pin for connection to the Arduino: the control pinsS0, S1, S2, S3 , and the pin OUT , ​​which is responsible for sending information.


The pin OE (Output Enable, or enabled / activated output) must be connected to GND, since the module will send information continuously Arduino.


In the circuit I added 3 LEDs in corresponding colors to light up when a particular color is recognized by the sensor. While working in this experiment only with the three primary colors, nothing prevents other combinations are used in the program, according to the RGB signal levels.

In the program, the S0 and S1 pins are placed at a high level and then switched the states S2 and S3 pins, which determine which photodiode is activated. See the table below for the combinations that determine the type of output frequency and also the pattern of activation of photodiodes:



// Program: Color Detector module using TCS230   
// changes and comments: blog.circuits4you.com
   
// Connection of TCS230 module pins   
 const  int s0 = 8 ;  
 const  int s1 = 9 ;  
 const  int s2 = 12 ;  
 const  int s3 = 11 ;  
 const  int out = 10 ;   
   
// Pin the leds   
 int pinLedRed = 2 ;  
 int pinLedGreen = 3 ;  
 int pinLedBlue = 4 ;  
    
// Variables that store the value of the colors   
int red = 0 ;  
 int green = 0 ;  
 int blue = 0 ;  
    
void setup ()   
{  
  pinMode (s0, OUTPUT);  
  pinMode (s1, OUTPUT);  
  pinMode (s2, OUTPUT);  
  pinMode (s3, OUTPUT);  
  pinMode (Out, INPUT);  
  pinMode (pinLedRed, OUTPUT);  
  pinMode (pinLedGreen, OUTPUT);  
  pinMode (pinLedBlue, OUTPUT);  
  Serial.begin ( 9600 );  
  digitalWrite (s0, HIGH);  
  digitalWrite (s1, HIGH);  
}  
    
void loop ()
{  
  color (); // Call the routine le colors   
  // Shows the serial monitor the values ​​detected   
  Serial.print ( "Red" );  
  Serial.print (red, DEC);  
  Serial.print ( "Green" );  
  Serial.print (green, DEC);  
  Serial.print ( "Blue" );  
  Serial.print (blue, DEC);  
  Serial.println ();  

  // Check if the red color was detected   
  if (red <blue red && <&& green red> 50 )  
  {  
   Serial.println ( "Red" );  
   digitalWrite (pinLedRed, HIGH); // Lights red LED  
   digitalWrite (pinLedGreen, LOW);  
   digitalWrite (pinLedBlue, LOW);  
  }  

  // Check if the blue color was detected   
  else  if (blue <&& red blue <green)   
  {  
   Serial.println ( "Blue" );  
   digitalWrite (pinLedRed, LOW);  
   digitalWrite (pinLedGreen, LOW);  
   digitalWrite (pinLedBlue, HIGH); // Lights blue LED  
  }  

  // Check if the color green was detected   
  else  if (green <red green && <blue)  
  {  
   Serial.println ( "Green" );  
   digitalWrite (pinLedRed, LOW);  
   digitalWrite (pinLedGreen, HIGH); // Turns on the green LED  
   digitalWrite (pinLedBlue, LOW);  
  }  
  Serial.println ();  

  // Wait 2 seconds, turns off the LEDs and restarts the process   
  delay ( 2000 );   
  digitalWrite (pinLedRed, LOW);  
  digitalWrite (pinLedGreen, LOW);  
  digitalWrite (pinLedBlue, LOW);  
 }  
    
void color ()  
{  
  // Routine that reads the value of the colors  
  digitalWrite (s2, LOW);  
  digitalWrite (s3, LOW);  
  // Count OUT, Pred, RED  
  red = pulseIn (out digitalRead (out) == HIGH LOW: HIGH);  
  digitalWrite (s3, HIGH);  
  // Count OUT, pBlue, BLUE  
  blue = pulseIn (out digitalRead (out) == HIGH LOW: HIGH);  
  digitalWrite (s2, HIGH);  
  // Count OUT, pGreen, GREEN  
  green = pulseIn (out digitalRead (out) == HIGH LOW: HIGH);  
}

Load the program and bring objects of different colors to the sensor. The corresponding LED will light up, and is also generated output like this in the serial monitor, where you can see exactly the signal levels of the color detected: See the output on terminal also.

Sound sensor Arduino

Tutorial on how to interface sound sensor with Arduino. More precisely, Sound Sensor. I will show how to detect ambient sound and how to handle the signal generated by this module. This board along with the microphone, has a small built-in amplifier (integrated circuit LM386), because only the microphone would not be able to send data for Arduino. The connection scheme is very clean, composed of only 3 pins: Vcc, GND and S (signal). In the middle of the plate, there is a potentiometer for sensitivity adjustment.
The board works with 5V voltage, and the signal pin should be connected preferably to an analog port of Arduino, since the generated signal is variable, and thus we can see the different levels of noise picked up by the microphone. The circuit tests will consist of the module Sound Sensor , plus the display 16x2 LCD , which we've covered here on the site, in this article . If you do not have this display, do not worry because the program is easily adaptable to other models of LCD displays . The display at its top, will show the sound level (Low, Medium and High), and bottom , a bar that will follow in real time the sound level detected by the microphone will be shown:

The program does not use any own library for the sound sensor module, as it will only make the reading of the variable signal received by the Arduino analog port. For the LCD display is used to already known LiquidCrystal .To improve the program's accuracy, I chose to read the sign 128 times using the NUM_Measure variable. Then calculate the mean value and use this value to generate the LCD sound level indication. The graph on the display bottom line uses the signal information in real time. If you choose not to use the display, the data is also shown in the Serial Monitor.

// Program: Noise Levels with Sound Sensor   
   
int num_Measure = 128 ; // Set the number of measurements   
int pinSignal = A0; // pin connected to pin O module sound sensor   
long Sound_signal;    // Store the value read Sound Sensor   
long sum = 0 ; // Store the total value of n measurements   
long level = 0 ; // Store the average value   
int mostranivel = 0 ; // Used to generate the graph below   
int delete = 0 ; // Variable assist to generate the graph below  
   
#include <LiquidCrystal.h> // Load the LCD library  
   
// Set the pins that will be connected to the LCD   
LiquidCrystal LCD ( 12 , 11 , 5 , 4 , 3 , 2 );
   
// Array to mount the symbol graphic   
byte [ 8 ] =   
{B10000, B11000, B11100, B11110, B11110, B11100, B11000, B10000,};   
   
void setup ()  
{   
  pinMode (pinSignal, INPUT); // Set the signal pin as input   
  Serial.begin ( 9600 );  
  lcd.begin ( 16 , 2 ); // Initialize LCD   
  lcd.clear ();    // Clear the LCD   
  lcd.setCursor ( 0 , 0 ); // Shifts the column 0, row 0   
  lcd.print ( "Level : " );  
  lcd.createChar ( 1 , a); // Assigns the "1" the value of the array "A",    
                        // drawing the symbol graph  
}  
   
void loop ()  
{  
  // Performs 128 signal readings   
  for ( int i = 0 ; i <num_Measure; i ++)  
  {  
    Sound_signal= analogRead (pinSignal);  
    sum  = sum + Sound_signal;  
  }  

  level = sum / num_Measure; // Calculate the average value   
  // Convert the read value to a value between 0 and 15   
  mostranivel = map (signal, 0 , 500 , 0 , 15 );    
    
  // Generate the graph below   
  for ( int i = 0 ; i <mostranivel; i ++)  
  {  
    lcd.setCursor (i, 1 );  
    lcd.write ( 1 );  
    erases = 15 - i;  
     for ( int x = i; x < 15 ; x ++)  
    {  
      lcd.setCursor (x + 1 , 1 );  
      lcd.write ( "" );  
    }  
  }  
     
  // Check the signal level and displays on the screen     
  if (level> 0 && level < 100 )  
  {  
    lcd.setCursor ( 8 , 0 );  
    lcd.print ( "Low" );  
    Serial.print ( "Low Level" );  
    Serial.print ( "- Media" );  
    Serial.println (level);  
  }  

  if (level> 100 && level < 200 )  
  {  
    lcd.setCursor ( 8 , 0 );  
    lcd.print ( "Medium" );  
    Serial.print ( "Medium Level" );  
    Serial.print ( "- Media" );  
    Serial.println (level);  
  }  

  if (level> 200 )  
  {  
    lcd.setCursor ( 8 , 0 );  
    lcd.print ( "High" );  
    Serial.print ( "High level" );  
    Serial.print ( "- Media" );  
    Serial.println (level);  
  }  

  sum = 0 ; // Reset the sum of the measurement values  
}  

You can adjust the level by changing set points. observe the output also on serial terminal

Arduino Temperature and BMP085 pressure - Module GY-65

Today I will show you how to interface BMP085 atmospheric pressure sensor module with arduino, which is the temperature module and BMP085 pressure , like the one below:


BMP085 sensor interface with arduino
The name BMP085 comes from the name given to the sensor itself, as we see in the datasheet . In the case of the photo above module, the module code is GY-65. The module works with a voltage of 1.8 to 3.6V. Its pressure reading range is 300 to 1100 hPa (hectoPascal), which determines altitudes from 9000 meters above sea level by -500m. The connection to the controller is made ​​by only 2 pins, using the I2C interface. I use a library Adafruit, which you can download at that address . A parenthesis about this library is that when decompressing, the folder name is "Adafruit-BMP085-Library-master" . Unfortunately , the IDE does not accept name folder libraries with numbers and special characters, then rename the folder just for " BMP085 ". . Use the 1.0.5 version of the Arduino IDE, I do not know if it happens in other versions Library downloaded and installed, go to the circuit:


BMP085 Sensor Interface with Arduino

Take care not to reverse any connection to connect the wires, because usually the marking pins comes under the plate, getting hidden when you fit into the breadboard. The program below is based on the test program that comes along with the library, with the due translations to facilitate understanding:

// Program: Pressure test module BMP085 
// Author: Adafruit 
// translations and commentaries: Arduino and Co.

#include <Wire.h> 
#include <Adafruit_BMP085.h>

// Connect Vcc pin of the BMP085 to the Arduino pin 3.3V (5.0V NOT USE!) 
// Connect GND pin module to the Arduino GND 
// Connect the SCL pin module to analog pin 5 Arduino 
// Connect pin SDA module to pin 4 of analog Arduino 
// pin EOC (end of conversion) unused 
// XCLR pin is a reset pin is not used

Adafruit_BMP085 bmp;

void setup ()
{
  Serial.begin ( 9600 );
   if (bmp.begin ()) {
  Serial.println ( "Sensor BMP085 not found, check the connections!" );
   While ( 1 ) {}
  }
}

void loop ()
{
    Serial.print ( "Temperature =" );
    Serial.print (bmp.readTemperature ());
    Serial.println ( "C *" );

    Serial.print ( "Pressure =" );
    Serial.print (bmp.readPressure ());
    Serial.println ( "Pa" );

    // Calculate the elevation using the barometric pressure pattern 
    // of 1013.25 millibar = 101325 Pascal 
    Serial.print ( "Altitude =" );
    Serial.print (bmp.readAltitude ());
    Serial.println ( "m" );

  // It is possible to draw a more accurate measurement if you know 
  // the pressure at sea level, which varies with time / climate. 
  // If it is 1015 millibars, equals 101,500 Pascals.

    Serial.print ( "real Altitude =" );
    Serial.print (bmp.readAltitude ( 101500 ));
    Serial.println ( "m" );
    Serial.println ();

    delay ( 5000 );
}

Run the program and open the serial monitor. Arduino Mega, you must use pin 20 (SDA) and 21 (SCL). 


Controlling a servo motor with Arduino

How to interface servo motor with arduino?
The great cheap to buy things online, and abroad, is the almost complete lack of textbooks. An example is the servo motor below.

A label says " Micro Servo 9g" was the biggest clue that I had the model. There begins the fun part of it, which is to find out how the servant works, their connections, voltages and such. Sifting through Google, I found a very similar model, called HXT900, and that its operating voltage ranges from 3 to 6V. It's a beginning. ? But what connections Well, the cable has 3 ways: Black, red and white. Easy, right? Black on GND, red on 5V . And white for data I tested the servo using the example that comes in the Arduino IDE, which uses a potentiometer to control the engine speed:

Arduino Servo motor interface

The program below you can find on the menu File -> Examples -> Servo -> Knob :



// Program: Controlling the position of a servant using potentiometer 
// Controlling the servo position using the potentiometer (variable resistor) 


#include <Servo.h> 
myservo Servant;   // create servo object to control a servo

// Analog pin potentiometer 
int potpin = 0 ;

// Variable that stores the read value of the potentiometer 
int val;     

void setup ()
{ 
  // Set the servo is connected to port 9 
  myservo.attach ( 9 );  
} 

void loop ()
{ 
  // Le value of potentiometer (values ​​between 0 and 1023) 
  val = analogRead (potpin);            

  // Convert the value to be used in servo (values ​​between 0 and 180) 
  val = map (val, 0 , 1023 , 0 , 179 );     

  // Move the axis servo, according to the angle
  myservo.write (val);                  

  // Wait for the servo to reach the position 
  delay ( 15 );                           
}

The program reads the pot of information connected to the analog input values ​​(0-1023), converts these values ​​to be used in servo (0-180), and moves the servo motor to the corresponding position. Turn the potentiometer in 2 -way, and the servant will accompany the pot movement. We can also control the servo using pre-established positions, which is very useful when we need to use the engine to repetitive movements. As in the example below, where I used 3 keys (push-button):



and the following program:

// Program: Controlling the servo with 3 keys 

#include <Servo.h> 

Servo myservo;

int val, 
 int ChE = 3 ; // pin to be connected in key left 
int ChC = 4 ;   // pin to be connected in the central key 
int ChD = 5 ;   // pin to be connected in the right key

void setup ()
{ 
  // Servo data pin connected to pin 9 of the Arduino 
  myservo.attach ( 9 );   
   // Set the pin as input
  pinMode (ChE, INPUT);      
  // Triggers the internal pull-up resistor
  digitalWrite (ChE, HIGH);  
  pinMode (ChC, INPUT);
  digitalWrite (ChC, HIGH);
  pinMode (ChD, INPUT);
  digitalWrite (ChD, HIGH);
} 

void loop ()
{ 
 // Le value of the Left Key (On / Off)
 val = digitalRead (ChE);  
   // If the key is pressed, the servo moves 
   if (val! = 1 )
    {
       myservo.write ( 60 );   // Move the servo to the angle of 60 degrees 
       delay ( 15 );           // Delay for the servant reach the position
    }

 val = digitalRead (ChC);
   if (val! = 1 )
    {
       myservo.write ( 90 );   // Move the servo to the angle of 90 degrees 
       delay ( 15 );
    }

 val = digitalRead (ChD);
   if (val! = 1 )
    {
       myservo.write ( 120 );   // Move the servo to the angle of 120 degrees 
       delay ( 15 );
    }
}

Pressing the left button, the servo moves to the "60 degrees", the center button takes the position 90 degrees, and the right button, the position 120 degrees.

IR remote control Arduino

IR Remote control for arduino reads the key code of IR Remote having NEC Protocol.

Components Required:
1. IR Sensor
2. Arduino Uno
3. Remote Control


Step 1: Circuit Connections

The connection of the IR receiver module shown in the image below, which is the connection of the module pins to port 11 of the Arduino (signal), 5v and GND. In the right image, we can see the module in operation, with an LED that flashes when the module is receiving infrared signals:

For the circuit we will use 2 LEDs, 2 resistors of 330 ohms and the IR receiver, taking care to observe the correct connection of the module pins, avoiding component of burning:


Arduino IR Remote

Step 2: Programming the arduino

A program that reads the IR signal received, and based on the value read, triggers the ports, where the LEDs are connected. This program also features the serial monitor the values ​​read by IR receiver:

This way you can find out the values ​​in hex on Serial terminal, the signal received by all remote control keys. In the program, used the values ​​FF30CF (Key 1), FF18E7 (key 2), FF10EF (key 4), FF38C7 ​​(key 5), and FF52AD (key 9) . Finally, in the loop, I test the value received by the IR receiver , and some commands IF I check which key was pressed, lighting and putting out the corresponding lED. I used the 9 key to erase the 2 LEDs at once. For this program, we will use the library irRemote , which you can download by clicking here :


// Program: Test IR Remote Control   
  
#include <IRremote.h>  
  
int RECV_PIN = 11 ;  
 float IRRemote;  
 int pinLED1 = 5 ;  
 int pinLED2 = 7 ;  
  
IRrecv irrecv (RECV_PIN);  
decode_results results;  
  
void setup ()  
{  
  pinMode (pinoledvermelho, OUTPUT);   
  pinMode (pinoledverde, OUTPUT);  
  Serial.begin ( 9600 );  
  irrecv.enableIRIn (); // Initialize the IR receiver  
}  
   
void loop ()  
{  
  if (irrecv.decode (& results))  
  {  
    Serial.print ( "read value" );  
    Serial.println (results.value, HEX);  
    IRRemote = (results.value);  
    if (IRRemote == 0xFF30CF ) // Check if the 1 key is pressed  
    {  
      digitalWrite (pinLED1, HIGH);   // Lights red LED  
    }  
    if (IRRemote == 0xFF18E7 ) // Check if the 2 key has been thrown  
    {  
      digitalWrite (pinLED1, LOW); // Off the red LED  
    }  
    if (IRRemote == 0xFF10EF ) // Check if the 4 key has been pressed  
    {  
      digitalWrite (pinLED2, HIGH); // Turns on the green LED  
    }  
    if (IRRemote == 0xFF38C7 ​​) // Check if the 5 button was pressed  
    {  
      digitalWrite (pinLED2, LOW); // Off the green LED  
    }  
    if (IRRemote == 0xFF52AD ) // Check if the key was thrown 9  
    {  
      digitalWrite (pinLED1, LOW); // Turn off all leds  
      digitalWrite (pinLED2, LOW);  
    }  
  irrecv.resume ();
  }  
}  

I2C module with LCD display using Arduino

How to interface PCF85574 module with arduino?
To connect LCD display 16x2 or 20x4 to Arduino you know you'll need at least 6 wires to connect, it means sacrificing some IO's that could be used for connecting other components such as sensors or motors. another way is to use 74HC595 Shift register for interfacing. In this tutorial we will discuss on PCF85574 I2C LCD Display Interface.

A module that can be used to circumvent this problem is the I2C Module for LCD Display with PCF85574 (datasheet ):

I2C LCD Module for arduino
With this module, you can control an LCD display, either 16x2 or 20x4, using only two pins Arduino: the analog input pin 4 (SDA) and the analog input pin 5 (SCL) forming the I2C interface.

Module structure I2C

In the module left side we have 4 pins, and two are for power ( Vcc and GND ), and the other two are the interfaceI2C ( SDA and SCL ) . The plate pot is for display contrast adjustment, and the jumper on the opposite side allows the back light is controlled by the program or remain off for power saving.

By default the module is configured with the address 0x27 , but you can change this address using the pins A0, A1 and A2.

Step 1: Connection of I2C LCD Module with Arduino

The module has 16 pins that can be directly connected to the display, or you can test the connection in breadboard, as I did riding the circuit below where I used a 16x2 LCD display with HD44780 controller connected to the Arduino Uno: If you are using an Arduino Mega 2560, use the pin 20 (SDA) and 21 (SCL) :

Arduino I2C LCD Module Connections

Step 2: Programming I2C LCD Module with Arduino


To control this I2C module, use the library LiquidCrystal_I2C available at this link . Unzip the file and rename the folder LiquidCrystal to LiquidCrystalI2C by copying it to the folder  LIBRARIES the IDE of your Arduino.Rename the folder prevents you from having conflicts with LiquidCrystal library that is already built into the IDE.

The commands for display control are almost the same library  LiquidCrystal we use normally with commands such as lcd.begin () , lcd.print ()   and lcd.setCursor () . In I2C library, the command lcd.setBacklight () alloy (HIGH ) or off ( LOW ) the display backlight.


// Program: 16x2 LCD display and I2C module 
// blog.cirucits4you.com

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Initialize the display at the address 0x27 
LiquidCrystal_I2C LCD ( 0x27 , 2 , 1 , 0 , 4 , 5 , 6 , 7 , 3 , POSITIVE);
 
void setup ()
{
 lcd.begin ( 16 , 2 );
}
 
void loop ()
{
  lcd.setBacklight (HIGH);
  lcd.setCursor ( 0 , 0 );
  lcd.print ( "circuits4you.com" );
  lcd.setCursor ( 0 , 1 );
  lcd.print ( "LCD and I2C module" );
  delay ( 1000 );
  lcd.setBacklight (LOW);
  delay ( 1000 );
}


Arduino display module 7-segment 8 digits with MAX7219

Tutorial on how to interface MAX7219 7 segment display module with arduino?

A module suggestion for those who need to use 7 segment display without having to connect a lot of wires, this display module 7 8 digit segments, with IC MAX7219 .
Arduino 7-Segment LED Interface

As you can see by the image, we can print some characters in this display and, of course, to show the numbers.Another way to trigger the display, which we shall see, is to use a sequence of bits to trigger the corresponding segments on the display and create our own symbols.


The operation of the 8-digit module is very similar to what we saw in these posts because it also uses only three pins (left) to connect the Arduino or other microcontroller 


The right side, we find the pins that allow the connection of several modules in cascade:

Pin Connections of 8 digit 7-segment MAX7219

The circuit for connection to Arduino this way is then, with the pins 5, 6 and 7 respectively connected to pinsLOAD , CLK and DIN :

Arduino Connections with MAX7219 7-segment LED


Program Library :

The library LedControl , which can be downloaded at this link is who will control the display of the drive. The main commands LedControl used in the program:

LedControl LedControl lc = (7, 6, 5, 1) = Sets the pins to be connected to the module. Pin 7 of the Arduino to DIN 6 pin to CLK and pin 5 to LOAD. The latter figure refers to the number of modules that are connected to the Arduino.

lc.shutdown (0, false) = This command also serves to initialize the display also serves to clear the display without losing any data in memory. This command is especially useful when the module is being powered by batteries, since we can show the data on the display only when triggered a button, for example. In this case, just change the last parameter to TRUE when you do not want the display to remain on.

lc.setIntensity (modulo, value) = regulates the brightness of the display. The first parameter is the module number, and value can be a number between 0 (minimum) to 15 (maximum brightness).

lc.setChar (modulo, digit, character, false) = Prints a character on the display. Despite not having many characters available due to limited display of 7 segments, we can use the characters A, B, C, D, E, F, H, L and P.

lc.setDigit (modulo, type, value, ponto_decimal) = used to directly send a number or the value of a variable for a given type in the display. The parameter ponto_decimal can receive the values ​​TRUE (displays the decimal point), or FALSE (disables the decimal point).

Arduino Code for MAX7219 Display Interface:

// Program: Display 7 segments - 8 digit MAX7219 
// Load the LedControl library 
#include "LedControl.h"

// Definitions pins and number of modules in the circuit 
LedControl lc = LedControl ( 7 , 6 , 5 , 1 );

void setup ()
{
  // Initialize the module 
  lc.shutdown ( 0 , false);
   // display brightness adjustment 
  lc.setIntensity ( 0 , 3 );
   // Delete the display 
  lc.clearDisplay ( 0 );
}

void loop ()
{
  // Send Arduino for display
  writeArduinoOn7Segment ();
  // Countdown
  countdown();

  // Init counter 0 1 million 
  for ( long i = 0 ; i < 1000000 ; i ++)
  {
    printNumber (i);
  }
  delay ( 500 );
}
void printNumber ( long v)
{
  // Variable value digit 
  int digito1;
   int digito2;
   int digito3;
   int digito4;
   int digito5;
   int digito6;
   int digito7;
   int digito8;

  // Calculate the value of each digit 
  digito1 v =% 10 ;
  digito2 = (V / 10 )% 10 ;
  digito3 = (V / 100 )% 10 ;  
  digito4 = (V / 1000 )% 10 ;
  digito5 = (V / 10000 )% 10 ;
  digito6 = (v / 100000 )% 10 ;
  digito7 = (v / 1000000 )% 10 ;
  digito8 = (v / 10000000 )% 10 ;
  
  // Display the value of each digit in the display 
  lc.setDigit ( 0 , 7 , (byte) digito8, false);
  lc.setDigit ( 0 , 6 , (byte) digito7, false);
  lc.setDigit ( 0 , 5 , (byte) digito6, false);
  lc.setDigit ( 0 , 4 , (byte) digito5, false);
  lc.setDigit ( 0 , 3 , (byte) digito4, false);
  lc.setDigit ( 0 , 2 , (byte) digito3, false);
  lc.setDigit ( 0 , 1 , (byte) digito2, false);
  lc.setDigit ( 0 , 0 , (byte) digito1, false);
  delay ( 00 );
}

void writeArduinoOn7Segment ()
{
  // Arduino on the display 
  for ( int i = 0 ; i < 21 ; i ++)
  {
    lc.setChar ( 0 , i, 'a' , false);
    lc.setRow ( 0 , i 1 , 0x05 );
    lc.setChar ( 0 , i- 2 , 'd' , false);
    lc.setRow ( 0 , i- 3 , 0x1c );
    lc.setRow ( 0 , i- 4 , B00010000);
    lc.setRow ( 0 , i 5 , 0x15 );
    lc.setRow ( 0 , i- 6 , 0x1D );
    lc.setChar ( 0 , i- 7 , '' , false);
    lc.setChar ( 0 , i- 8 , 'and' , false);
    lc.setChar ( 0 , i- 9 , '' , false);
    lc.setRow ( 0 , i- 10 , B1001110);    
    lc.setRow ( 0 , i- 11 , B0010000);  
    lc.setChar ( 0 , i- 12 , 'a' , false);    
    lc.setChar ( 0 , i- 13 , '' , false);
    delay ( 300 );
  }
}


Finally, to activate only certain segments of the display, use the command lc.setRow () , which has the following syntax:

lc.setRow (numero_modulo, I type, value) = We can define value in decimal, hex or binary, but the command is easier to understand if we use binary.

See for example the letter "R", we write on the display using this command: lc.setRow (0, I type, 0x05) In binary, the command looks like this: lc.setRow (0, digit, B00000101 )

That means that we are activating only the segments E and G of the display.