Controlling a Seven-Segment Display Using Arduino Part 4 of 4

Posted February 22nd, 2010 by admin and filed in Arduino

The third and final sketch uses bits to represent each segment and is a reduced code version of the previous sketch (1,210 bytes for sketch #3 instead of 1,852 bytes for sketch #2). A ten element array holds a byte for each number 0-9 that specifies what segments should be lit (pin low). Bit 0 corresponds to segment A, bit 1 to segment B and so on. In order to display the number 1, segments B and C need to be lit, so that is represented by the value 0b1111001. Function “lightSegments” reads these bits in sequence and sets the corresponding segments accordingly.

Sketch #3:

// Seven-segment LED Display
// Common Anode pins 3 and 8

//   G F + A B
//   | | | | |   -> pins and segments they control
//   ---------
//  F|   A   |B
//   |---G---|   -> segments
//  E|   D   |C
//   ---------
//   | | | | |   -> pins and segments they control
//   E D + C DP

// Segments that make each number when lit:
// 0 => -FEDCBA
// 1 => ----BC-
// 2 => G-ED-BA
// 3 => G--DCBA
// 4 => GF--CB-
// 5 => GF-DC-A
// 6 => GFEDC-A
// 7 => ----CBA
// 8 => GFEDCBA
// 9 => GF-DCBA

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define F 7
#define G 8

// Pushbutton connected to pin 9
#define BUTTON 9

int count = 0; // current display count

const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010, 0b0000010, 0b1111000, 0b0000000, 0b0010000 };

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BUTTON, INPUT);
  lightSegments(0b1000000);
}

void loop() {
  int val = digitalRead(BUTTON);
  if (val == HIGH) {
    count++;
    if (count == 10) count = 0;
    delay(200);
    lightSegments(numbers[count]);
  }
}

void lightSegments(byte number) {
  for (int i = 0; i < 7; i++) {
    int bit = bitRead(number, i);
    // segments connected to Arduino pins 2-8
    digitalWrite(i+2, bit);
  }
}

Watch a short video of this project in action.

Controlling a Seven-Segment Display Using Arduino Part 3 of 4

Posted February 15th, 2010 by admin and filed in Arduino

The second sketch cycles through the numbers from 0 to 9, but only increments the display counter each time a button is pressed. Note that this code includes simple debouncing by introducing a short delay when the Arduino detects that the button has been pressed.

Sketch #2:

// Seven-segment LED Display
// Common Anode pins 3 and 8

//   G F + A B
//   | | | | |   -> pins and segments they control
//   ---------
//  F|   A   |B
//   |---G---|   -> segments
//  E|   D   |C
//   ---------
//   | | | | |   -> pins and segments they control
//   E D + C DP

// Segments that make each number when lit:
// 0 => ABCDEF
// 1 => BC
// 2 => ABDEG
// 3 => ABCDG
// 4 => BCFG
// 5 => ACDFG
// 6 => ACDEFG
// 7 => ABC
// 8 => ABCDEFG
// 9 => ABCDFG

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define F 7
#define G 8

// Pushbutton connected to pin 9
#define BUTTON 9

// Common anode;
// on when pin is low
// and off when pin is high
#define ON LOW
#define OFF HIGH

int count = 0; // current display count
int val = 0;   // digital input from button

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BUTTON, INPUT);
  zero();
}

void loop() {
  val = digitalRead(BUTTON);
  if (val == HIGH) {
    count++;
    delay(200);
    switch (count) {
      case 0:
        zero();
        break;
      case 1:
        one();
        break;
      case 2:
        two();
        break;
      case 3:
        three();
        break;
      case 4:
        four();
        break;
      case 5:
        five();
        break;
      case 6:
        six();
        break;
      case 7:
        seven();
        break;
      case 8:
        eight();
        break;
      case 9: {
        nine();
        count = -1;
        break;
      }
    }
  }
}

// 0 => ABCDEF
void zero() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, OFF);
}

// 1 => BC
void one() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, OFF);
}

// 2 => ABDEG
void two() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, OFF);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, OFF);
  digitalWrite(G, ON);
}

// 3 => ABCDG
void three() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, ON);
}

// 4 => BCFG
void four() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

// 5 => ACDFG
void five() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

// 6 => ACDEFG
void six() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

// 7 => ABC
void seven() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, OFF);
}

// 8 => ABCDEFG
void eight() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

// 9 => ABCDFG
void nine() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

Controlling a Seven-Segment Display Using Arduino Part 2 of 4

Posted February 8th, 2010 by admin and filed in Arduino

This month’s project is a simple program to display the Arabic numbers using the Arduino and a 7-segment display.

Parts list:

The 7-segment display used in this example is a common anode display with pin connections as shown in the picture, and the Arduino sketches were written so as to light up a segment when the corresponding pin is LOW.

The first of a series of three simple sketches cycles through the numbers from 0 to 9, resets the display to show a “0″ once it reaches “9″ and repeats until the power is turned off on the Arduino. The code below accomplishes this in a very simplified manner, as each digit is formed by a separate function that sequentially lights up each of the necessary segments to display that number.

The third sketch in this sequence will use individual bits to represent each segment and will require less written code, a good thing to keep in mind when programming microcontrollers, as they have a small memory footprint (the Arduino Duemilanove has 32K bytes of program memory).

Sketch #1:

// Seven-segment LED Display
// Common Anode pins 3 and 8

//   G F + A B
//   | | | | |   -> pins and segments they control
//   ---------
//  F|   A   |B
//   |---G---|   -> segments
//  E|   D   |C
//   ---------
//   | | | | |   -> pins and segments they control
//   E D + C DP

// Segments that make each number when lit:
// 0 => ABCDEF
// 1 => BC
// 2 => ABDEG
// 3 => ABCDG
// 4 => BCFG
// 5 => ACDFG
// 6 => ACDEFG
// 7 => ABC
// 8 => ABCDEFG
// 9 => ABCDFG

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define F 7
#define G 8

// Common anode;
// on when pin is low
// and off when pin is high
#define ON LOW
#define OFF HIGH

int ms = 1000;

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
}

void loop() {
  zero();
  one();
  two();
  three();
  four();
  five();
  six();
  seven();
  eight();
  nine();
}

// 0 => ABCDEF
void zero() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, OFF);
  delay(ms);
}

// 1 => BC
void one() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, OFF);
  delay(ms);
}

// 2 => ABDEG
void two() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, OFF);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, OFF);
  digitalWrite(G, ON);
  delay(ms);
}

// 3 => ABCDG
void three() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, ON);
  delay(ms);
}

// 4 => BCFG
void four() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
  delay(ms);
}

// 5 => ACDFG
void five() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
  delay(ms);
}

// 6 => ACDEFG
void six() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
  delay(ms);
}

// 7 => ABC
void seven() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, OFF);
  delay(ms);
}

// 8 => ABCDEFG
void eight() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
  delay(ms);
}

// 9 => ABCDFG
void nine() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
  delay(ms);
}

Controlling a Seven-Segment Display Using Arduino Part 1 of 4

Posted February 1st, 2010 by admin and filed in Arduino

A seven segment display is composed of seven elements that individually on or off can be combined to produce simplified representations of the numbers 0-9.

There are two types of 7-segment displays:

  • Common Anode with all the LED anodes connected together. These need a display driver with outputs which become low to light each segment. The common anode is connected to +Vs.
  • Common Cathode with all the LED cathodes connected together. These need a display driver with outputs which become high to light each segment. The common cathode is connected to 0V.

The segments in a 7-segment display are referred to by the letters A-G, as shown in the picture. Some displays have a dot to allow for the representation of a decimal point.

Basic Hobby Electronics Vocabulary Part 4 of 4 (S-V)

Posted January 25th, 2010 by admin and filed in Beginner

Schematic: A diagram of an electrical circuit that uses standardized symbols for the components.

Semiconductor: A material of electrical resistance between that of a conductor and an insulator. It is used to construct diodes, transistors, and integrated circuits.

Solder: A tin-lead alloy that becomes liquid when heated to above 360 degrees. It has low resistance, like other metals, and provides a strong mounting.

Switch: A device used to connect or disconnect the wires in an electric circuit, turning it on or off.

Transistor: A three-terminal, solid-state electronic device designed to amplify, oscillate, or switch the flow of current between two terminals.

Voltage: The measure of difference of electric potential across a material or between two points in a circuit.

Volts (V): The unit of measure for voltage.

Basic Hobby Electronics Vocabulary Part 3 of 4 (I-R)

Posted January 18th, 2010 by admin and filed in Beginner

Insulator: A material that has high electrical resistance and is therefore a poor conductor of electricity.

Integrated Circuit (IC): A type of digital circuit in which transistors, diodes, resistors and capacitors are constructed on a semiconductor base.

Light Emitting Diode (LED): A type of diode that generates light when current flows through it.

Ohm’s Law: The relationship between voltage, current and resistance.

Ohm (Ω): The unit of measure for resistance.

Printed Circuit Board (PCB): A board in which components are connected using a thin coat of conductive material “printed” on the board instead of wires. It is used for mounting electrical components.

Resistance: The electrical friction between an electric current and the material it is flowing through that causes electricity to be dissipated as heat.

Resistor: An electrical component used to introduce resistance into a circuit.

Basic Hobby Electronics Vocabulary Part 2 of 4 (D-G)

Posted January 11th, 2010 by admin and filed in Beginner

Digital Circuit: A circuit in which inputs and outputs have only two possible states: low (0) or high (1).

Diode: A two-terminal electronic device that allows current to flow in only one direction.

Direct Current (DC): Current that flows across a material in one direction only.

Disc Capacitor: A type of capacitor that has low capacitance and is used mostly in high frequency circuits. Disc capacitors are not polarized.

Electrolytic Capacitor: A type of capacitor that has high capacitance and is used mostly in low frequency circuits. Electrolytic capacitors are polarized.

Electronics: The science and technology concerned with and based on electricity and its applications.

Farad (F): The unit of measure for capacitance.

Ground: A common name for the reference point in an electrical circuit at which the measured voltage is taken to be zero.

Basic Hobby Electronics Vocabulary Part 1 of 4 (A-C)

Posted January 4th, 2010 by admin and filed in Beginner
If you’re a beginner to hobby electronics, here is a short list of the most basic terms you will encounter as you venture into your electronics hobby.

Alternating Current (AC): Current that is periodically reversing its direction of flow.

Ampere (A): The unit of measure for electric current. Commonly shortened to “amp”.

battery

Battery: A device which uses a chemical reaction to convert chemical energy directly into electrical energy.

Capacitance: A measure of the electric charge that can be stored on a conductor; the ability of such conductors to store electric charge.

Capacitor: An electrical component that introduces capacitance in electric circuits.

Conductor: A material that has low electrical resistance and can therefore efficiently allow electrical current to flow through it.

Current: A measure of the flow of electric charge passing any point of a wire per unit of time.

Check Out the Arduino Contest Winners

Posted December 27th, 2009 by admin and filed in Arduino, Contests

The Arduino Contest Winners were recently announced. The grand prize winner was “the Word Clock“, a clock that uses language to tell the time.

Binary clocks, gardening, a talking robot head, a coffee roaster, a digital window sticker, and a Wii Nunchuck adapter are just a few of the cool ideas submitted. Each is presented as a how-to “Instructable” so that you can build your own.

Check out the winning projects at the Instructables website.

Circuit Cellar Launches 2010 Design Contest

Posted December 17th, 2009 by admin and filed in Contests

The iMCU Design Contest 2010 is the second contest WIZnet has co-sponsored with Circuit Cellar. The iEthernet Design contest held in 2007 introduced the W5100 hardwired TCP/IP Ethernet controller to the design community. The W5100 allowed designers to finally bring Ethernet capability to their existing embedded projects.

The 2010 design contest requires the use of Wiznet’s W7100 as part of an embedded project. The W7100 is an Internet microcontroller integrating a hardwired TCP/IP core with an 8051 processor. The new W7100 chip provides a platform for applications that need a network connection, and its MCU capabilities help engineers create sophisticated Internet-enabled projects.

Circuit Cellar is a print magazine about hands-on embedded systems projects, so every iMCU Design Contest entrant is viewed as a potential author. Many of the participants with the best entries may become Circuit Cellar authors, project evaluators or advisors, which is quite a resume booster opportunity.

Contest judges will look at a wide range of criteria when selecting the winning projects, including originality, technical merit, usefulness, cost-effectiveness and design optimization. The deadline for project submission is June 30, 2010. For more information visit www.circuitcellar.com/iMCU.