Showing posts with label beginner. Show all posts
Showing posts with label beginner. Show all posts

May 25, 2016

Interact with Arduino through serial

In this post I will show how to use the USB serial interface to issue commands and retrieve output from an Arduino board. The main goal of this project is to build a simple interface to use Arduino as a 'sensors hub' and integrate it with other more complex devices like Raspberry PI or and PC.

Now plug your Arduino board to the PC and upload the following sketch.


int PIN_LED = 13;

void setup()
{
  pinMode(PIN_LED, OUTPUT);
  
  Serial.begin(9600);

  Serial.println("Welcome to ArduinoSerialControl");
  Serial.println("Enter 't' to retrieve current millis");
  Serial.println("Enter 'l' to toggle LED on pin 13");
}

void loop()
{
  int bytes=Serial.available();
  if (bytes > 0)
  {
    char c = Serial.read();
    switch (c)
    {
    case 't':
      // return the uptime in milliseconds
      Serial.print("millis=");
      Serial.println(millis());
      break;
    case 'l':
      // toggle on-board led connected to pin 13
      digitalWrite(PIN_LED, !digitalRead(PIN_LED));
      Serial.print("LED status is ");
      Serial.println(digitalRead(PIN_LED));
      break;
    }
  }
}


Open the serial monitor and set the baud rate to 9600. Try sending commands using the serial console.



This is just an experiment. If you want to build a solid communication using Firmata protocol.

Complete Control Of An Arduino Via Serial

May 2, 2015

Beating heart with Arduino and a MAX7219 8x8 LED matrix

This is a very simple project to display a beating heart using and Arduino board and a 8x8 LED matrix driven by a MAX7219 chip.



The wiring is very simple.
  • MAX7219 VCC pin > Arduino 5V pin
  • MAX7219 GND pin > Arduino GND pin
  • MAX7219 DIN pin > Arduino pin 2
  • MAX7219 CS pin > Arduino pin 3
  • MAX7219 CLOCK pin > Arduino pin 4

The Arduino sketch is not using any library so this is also good to understand how to directly drive the MAX7219 chip through registers.


int ANIMDELAY = 100;  // animation delay, deafault value is 100
int INTENSITYMIN = 0; // minimum brightness, valid range [0,15]
int INTENSITYMAX = 8; // maximum brightness, valid range [0,15]

int DIN_PIN = 2;      // data in pin
int CS_PIN = 3;       // load (CS) pin
int CLK_PIN = 4;      // clock pin

// MAX7219 registers
byte MAXREG_DECODEMODE = 0x09;
byte MAXREG_INTENSITY  = 0x0a;
byte MAXREG_SCANLIMIT  = 0x0b;
byte MAXREG_SHUTDOWN   = 0x0c;
byte MAXREG_DISPTEST   = 0x0f;

const unsigned char heart[] =
{
  B01100110,
  B11111111,
  B11111111,
  B11111111,
  B01111110,
  B00111100,
  B00011000,
  B00000000
};



void setup ()
{
  pinMode(DIN_PIN, OUTPUT);
  pinMode(CLK_PIN, OUTPUT);
  pinMode(CS_PIN, OUTPUT);

  // initialization of the MAX7219
  setRegistry(MAXREG_SCANLIMIT, 0x07);
  setRegistry(MAXREG_DECODEMODE, 0x00);  // using an led matrix (not digits)
  setRegistry(MAXREG_SHUTDOWN, 0x01);    // not in shutdown mode
  setRegistry(MAXREG_DISPTEST, 0x00);    // no display test
  setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMIN);

  // draw hearth
  setRegistry(1, heart[0]);
  setRegistry(2, heart[1]);
  setRegistry(3, heart[2]);
  setRegistry(4, heart[3]);
  setRegistry(5, heart[4]);
  setRegistry(6, heart[5]);
  setRegistry(7, heart[6]);
  setRegistry(8, heart[7]);
}


void loop ()
{
  // second beat
  setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMAX);
  delay(ANIMDELAY);
  
  // switch off
  setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMIN);
  delay(ANIMDELAY);
  
  // second beat
  setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMAX);
  delay(ANIMDELAY);
  
  // switch off
  setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMIN);
  delay(ANIMDELAY*6);
}


void setRegistry(byte reg, byte value)
{
  digitalWrite(CS_PIN, LOW);

  putByte(reg);   // specify register
  putByte(value); // send data

  digitalWrite(CS_PIN, LOW);
  digitalWrite(CS_PIN, HIGH);
}

void putByte(byte data)
{
  byte i = 8;
  byte mask;
  while (i > 0)
  {
    mask = 0x01 << (i - 1);        // get bitmask
    digitalWrite( CLK_PIN, LOW);   // tick
    if (data & mask)               // choose bit
      digitalWrite(DIN_PIN, HIGH); // send 1
    else
      digitalWrite(DIN_PIN, LOW);  // send 0
    digitalWrite(CLK_PIN, HIGH);   // tock
    --i;                           // move to lesser bit
  }
}

January 11, 2015

Driving a LED strip with Arduino




Do you know those cheap LED strips?

These are often called "analog" LED strips because you cannot control each pixel individually like you do with "digital" strips.


Today we want to drive them using our Arduino board.



Hardware

  • Arduino Board
  • MOSFET
  • White LED strip

As MOSFET i have used I have used the IRF520 that comes with the Arduino starter kit but you can use any general purpose MOSFET (for example the popular STP16NF06).

Schematic









Code


#define PIN 11      // control pin
#define DELAY 10    // 20ms internal delay; increase for slower fades

void setup() {
  pinMode(PIN, OUTPUT);
}

void loop() {
  // fade in
  for(int i=0; i<255; i++) {
    analogWrite(PIN, i);
    delay(DELAY);
  }

  // fade out
  for(int i=0; i<255; i++) {
    analogWrite(PIN, 255-i);
    delay(DELAY);
  }
}

This is the code if you prefer a "strobo effect".

#define PIN 11      // control pin
#define DELAY 20    // 20ms internal delay; increase for slower pulse

void setup() {
  pinMode(PIN, OUTPUT);
}

void loop() {
  digitalWrite(PIN, HIGH);  // turn on
  delay(DELAY);             // wait few milliseconds
  digitalWrite(PIN, LOW);   // turn off
  delay(DELAY*10);          // wait some more time
}


References


Dimming a 12V LED Strip With an Arduino
RGB LED Strips
RGB Led Strip Controlled by an Arduino


January 10, 2015

Fading colors with an RGB LED

In this post I will explain how to make a simple fading RGB LED.
The code smoothly fades an RGB LED using PWM through 3 different basic colors (red, green and blue).

Hardware

  • Arduino Board
  • RGB LED
  • 3x Resistors 220 Ohm

Schematic

The schematic is very simple. Just connect the common cathod (+) to the GND pin. Then connect the 3 other leads to 3 PWM pins on your Arduino with a 220 Ohm resistor between.






Code


#define RED_PIN 9      // where the red pin is connected to
#define GREEN_PIN 10   // where the green pin is connected to
#define BLUE_PIN 11    // where the blue pin is connected to
#define DELAY 20       // 20ms internal delay; increase for slower fades

void setup() {
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  pinMode(RED_PIN, OUTPUT);
}

void loop() {
  // fade from green to red
  for(int i=0; i<255; i++) {
    analogWrite(RED_PIN, i);
    analogWrite(GREEN_PIN, 255-i);
    analogWrite(BLUE_PIN, 0);
    delay(DELAY);
  }

  // fade from red to blue
  for(int i=0; i<255; i++) {
    analogWrite(RED_PIN, 255-i);
    analogWrite(GREEN_PIN, 0);
    analogWrite(BLUE_PIN, i);
    delay(DELAY);
  }

  // fade from blue to green
  for(int i=0; i<255; i++) {
    analogWrite(RED_PIN, 0);
    analogWrite(GREEN_PIN, i);
    analogWrite(BLUE_PIN, 255-i);
    delay(DELAY);
  }
}

References

Fading RGB LED (Arduino)
Arduino ColorCrossfader
RGB LEDs
Make an RGB LED Fader

January 9, 2015

LED Blink

This is the very first project of every Arduino owner... make a LED blink.


Hardware

  • Arduino Board
  • LED
  • 220 Ohm resistor

Schematic



Circuit



Code


int pinLed = 13;

void setup() {                
  pinMode(led, OUTPUT);     
}

void loop() {
  digitalWrite(pinLed, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                  // wait for a second
  digitalWrite(pinLed, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                  // wait for a second
}

References

Arduino Blink example
Instructables tutorial

https://www.sparkfun.com/tutorials/219

January 8, 2015

LED basics

A light-emitting diode (LED) is a diode which emits light when activated. To limit the current flowing into the diode, it is important to introduce a current-limiting resistor in the loop.

The value of the appropriate resistor can be calculated with the following formula:

R=(V-Vd)/Id

where Vd is the voltage drop and Id is the current you want to use on your LED, usually the forward current. Those values depends on the specs of each LED and varies depending on the color of the LED.

So… you just want to light up an LED. What resistor should you use?

We can can make some rough assumptions to simplify the calculation:
  • Common LEDs runs at 20m.
  • The voltage drop is typically around 3V.
  • Arduino runs at 5V
This lead to the following calculation:
R = (5-3)/0.02 = 100 (Ohm)

We can say that 100 Ohms is the absolute minimum resistance we need to make sure that we do not damage the LED. To be safe, it's a good idea to use something a little higher, just in case your LED has slightly different ratings that what I've used here.
I always use 220 Ohm resistors because they are a safe choice for all kind of LEDs and are easy to find.

If you know the ratings of your LED (you can find it on the LED's datasheet) and you want to do this calculation yourself or use an online calculator like this or this.

References

All about LEDS
LED resistance calculator
Arduino Blink example
Instructables tutorial
Turn on LED with 5V
LED Current Limiting Resistors