Wednesday, February 18, 2015

Minimalistic Arduino

In order to build a minimalistic Arduino, you need:
  • one 10kOhm resistor
  • two 22pF capacitors
  • one 100nF capacitor
  • one 16.000Mhz quartz oscillator
  • the ATMEGA 328P-chip (best with preinstalled bootloader)
  • a 5V Voltage-supply and some wiring
  • something for programming the chip (existing Arduino-Board or a USB-FTDI-Serial converter)
optional:
  • one LED and a 150 Ohm resistor (two of both if you want one for the voltage supply as well)
  • switch (Reset switch)
  • USB-FTDI-Serialchip / Arduinoboard
the basic schematic:
minimalistic Ardunio schematic (with optional reset-switch)
If you like the default LED on PIN 13 on the original Arduinoboard, connect first the LED to Pin 19  (right after AVCC), then the 150 Ohm resistor to the LED and the other end to ground.

If you want to supply your project with for example 9V from a battery you can build a simple voltageregulator like this (not suited for AC-Input!):

simple DC-Voltageregulator
all you need is a LM7805 and two 10uF capacitors. If you want, you can connect a LED with resistor to the 5V output (in order to see if the Atmega is under power).

If you are using a USB-FTDI-Serialchip (UFSC) connect the DTR/RST-PIN to the RESET-Pin of the Atmega via a 100nF capacitor. RX of the UFSC goes to the TX of the Atmega and the TX of the UFSC goes to the RX-Pin of the Atmega.

Sunday, February 8, 2015

Arduino & EasyDriver & Steppermotor

If you'd like to run a steppermotor with your Arduino, you're going to need a EasyDriver. This chip allows you to connect a variety of different steppermotors and supply them with their needed voltage (in most cases 12 V). Most common steppermotors will have four wires comming out of them which connect as follows:

Wiring of the EasyDriver, the Arduino and the steppermotor (LED is optional)

If you have a motor with six wires comming out of it, try connecting the two outer and the two inner wires to the chip.

The following Arduinocode has been modified so that you can connect up to three EasyDriver-chips. If you like you can connect three LEDs to Pins 11, 12 and 13 in order to see which motor is supposed to be running (don't forget to use appropriate resistors).

//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Using the easy stepper with your arduino
//use rotate and/or rotateDeg to controll stepper motor
//speed is any number from .01 -> 1 with 1 being fastest - 
//Slower Speed == Stronger movement
//code-change (2015): run up to three motors:
//motor 1: connected to PIN 2 and 3
//motor 2: connected to PIN 4 and 5
//motor 3: connected to PIN 6 and 7
//connect LEDs to PIN 11,12 and 13 to see which motor is supposed to be running
/////////////////////////////////////////////////////////////////

#define DIR_PIN 2
#define STEP_PIN 3
#define DIR_PINt 4
#define STEP_PINt 5
#define DIR_PINth 6
#define STEP_PINth 7

void setup() 
{
  pinMode(DIR_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PINt, OUTPUT);
  pinMode(STEP_PINt, OUTPUT);
  pinMode(DIR_PINth, OUTPUT);
  pinMode(STEP_PINth, OUTPUT);
  pinMode(13,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(11,OUTPUT);
} 

void loop()
{ 

  for(int i=0; i<3; i++)
  {
    int randNumber;
    randomSeed(millis()); 
    randNumber = random(720)-360; // Random Number between -360 and 360
  
    //rotate a specific number of degrees 
    rotateDeg(randNumber, 0.7, i);
    delay(1000);
    rotateDeg(-randNumber, 0.7, i);
    delay(1000);  
  }
  //delay(1000);
  //rotate a specific number of microsteps (8 microsteps per step)
  //a 200 step stepper would take 1600 micro steps for one full revolution
  /*  
  rotate(8, .75, 1);
  delay(1000); 

  rotate(-1600, .75, 1); //reverse
  delay(1000);*/
}

void rotate(int steps, float speed, int motor)
{
  //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (steps > 0)? HIGH:LOW;
  steps = abs(steps);

  switch(motor){
    case 1:
    digitalWrite(DIR_PIN,dir); 
    break;
    case 2:
    digitalWrite(DIR_PINt,dir); 
    break;
    case 3:
    digitalWrite(DIR_PINth,dir); 
    break;
    default:
    break;
  }
  
  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++)
  {
    switch(motor){
      case 1:
      digitalWrite(STEP_PIN, HIGH);
      digitalWrite(13,HIGH);
      delayMicroseconds(usDelay);
      digitalWrite(STEP_PIN, LOW);
      digitalWrite(13,LOW);
      break;
      case 2:
      digitalWrite(STEP_PINt, HIGH);
      digitalWrite(12,HIGH);
      delayMicroseconds(usDelay); 
      digitalWrite(12,LOW);
      digitalWrite(STEP_PINt, LOW);      
      break;
      case 3:
      digitalWrite(STEP_PINth, HIGH);
      digitalWrite(11,HIGH);
      delayMicroseconds(usDelay); 
      digitalWrite(11,LOW);
      digitalWrite(STEP_PINth, LOW);
      break;
      default:
      break;
    }

    delayMicroseconds(usDelay);
  }
} 

void rotateDeg(float deg, float speed, int motor)
{
  //rotate a specific number of degrees (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  //motor specifies which motor to turn (number between 1 and 3)
  int dir = (deg > 0)? HIGH:LOW;
  switch(motor){
    case 1:
    digitalWrite(DIR_PIN,dir); 
    break;
    case 2:
    digitalWrite(DIR_PINt,dir);
    break;
    case 3:
    digitalWrite(DIR_PINth,dir);
    break;
    default:
    break;
  }
 
  int steps = abs(deg)*(1/0.225);
  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){
    switch(motor){
      case 1:
      digitalWrite(13,HIGH);
      digitalWrite(STEP_PIN, HIGH);
      delayMicroseconds(usDelay); 
      digitalWrite(13,LOW);
      digitalWrite(STEP_PIN, LOW);
      break;
      case 2:
      digitalWrite(STEP_PINt, HIGH);
      digitalWrite(12,HIGH);
      delayMicroseconds(usDelay); 
      digitalWrite(12,LOW);
      digitalWrite(STEP_PINt, LOW);
      break;
      case 3:
      digitalWrite(STEP_PINth, HIGH);
      digitalWrite(11,HIGH);
      delayMicroseconds(usDelay); 
      digitalWrite(11,LOW);
      digitalWrite(STEP_PINth, LOW);
      break;
      default:
      break;
    }
    delayMicroseconds(usDelay);
  }
}

The EasyDriver has a few more connectors you can use:
 
EasyDriver-chip

Most important are ENABLE and SLP (Sleep). You can connect each of them to a seperate Arduino PIN and use the digitalWrite(PIN, HIGH) command to activate them. The difference between these two:
  • ENABLE will stop the powersupply to the steppermotor (you will be able to turn the motor shaft by hand), the IC is still under power.
  • SLP will stop the powersupply of the motor and will put the IC into a hibernate-mode. Waking the IC up again can take some time (a few ms)

Monday, September 22, 2014

Arduino, QC1602A and HC-SR04

There are two ways to connect a LCD-Display and an Arduino: Via I2C or by connecting them as follows:

Wiring of the Arduino, HC-SR04 and the LCD-Display


The Arduino-Code:
#include 
/*
LCD-Connection:
VSS to GND
VDD to +5V
VO to potentiometer to GND
RS to 12
RW to 11
E to 10
D4 to 5
D5 to 4
D6 to 3
D7 to 2
A to 13 
K to GND

HC-SR04-Connection:
VCC to +5V
Trig to 8
Echo to 7
GND to GND

LED-Connection:
(GND-Resistor-LED)
green LED to 6
red LED to 9
*/


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int backLight = 13; //pin 13 für Hintergrundbeleuchtung


#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define erPin 9 //Pin fur rote LED (zu weit weg)
#define goPin 6 // Pin fuer grüne LED (nahe genug)

int maximumRange = 2000; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup()
{
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(erPin, OUTPUT);
  pinMode(goPin,OUTPUT);
  
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  //lcd.print("hello, world!");
  
}


void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in mm) based on the speed of sound.
 distance = duration/5.82;
 
 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.clear();
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("-1");
  digitalWrite(erPin, HIGH);
  digitalWrite(goPin, LOW);
  
 
 //Serial.println("-1");
 //digitalWrite(LEDPin, HIGH); 
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.clear();
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(distance);
  digitalWrite(erPin,LOW);
  digitalWrite(goPin, HIGH);
 
 
// Serial.println(distance);
 //digitalWrite(LEDPin, LOW); 
 }
 
 //Delay 50ms before next reading.
 delay(100);
}