AdSense

Sunday, 23 March 2014

Mobile phone - camera occlusion scratched, photos blurred

(Deutsche Version) My brother owns an HTC Desire Z. For quite some time, all the photos he takes are blurred. Looking at the camera occlusion reveals the reason: The centre of the occlusion is scratched, only the edge looks ok.
Slightly scratched camera occlusion, picture taken through a polished camera occlusion.
The problem is the anti reflection coating on the camera occlution. On the one hand, this creates nice pictures, but when it is scratched, it is nothing worth at all.

The solution for the problem is now to polish the whole coating away. Therefore you have to take some toothpaste and a cotton bud and rub it for about 2 minutes. Afterwards it should look like this:
Polished camera occlusion, picture taken through a not polished camera occlusion.
There is a difference in the qualities of the shown pictures. Nevertheless the quality after polishing is much better compared to a scratched camera occlusion where all pictures were blurred.

Sunday, 2 March 2014

Register pointer - accessing specific registers via I2C with Arduino

(Deutsche Version) Sometimes when using I2C you want to access a specific register of your I2C-save for reading and writing, e.g. when usingthe real time clock DS1307. This can be done using the so called register pointers. As you cann guess by the name, these pointers point to the register you want to read or write.

This is pretty easy to use: when writing via I2C, the first byte written with the write()-command sets the register pointer to the desired register. An example using the DS1307:
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(0x02); //set pointer to register 2
  Wire.write(decToBcd(hour)); //write value for hours in register 2
  Wire.write(decToBcd(weekDay)); //write value for weekday in register 3    
  Wire.endTransmission();

The first byte written after beginTransmission() sets the pointer to register 2 (where the hours are stored). With the second write()-command the value itself is written to the register. Then the pointer is automatically set to the next register, so the following write()-command writes to register 3.

Reading data from a specific register works similar:

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(0x02); //set pointer to register 2
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);//begin data request from register 2
  int hour = bcdToDec(Wire.read()); //read hours from register 2
  int weekDay = bcdToDec(Wire.read()); //read weekday from register 3
  
Like in the first example, the pointer is set to register 2. Then we can request data using the reguestFrom()-command. After that, read() reads data from register 2. After reading, the pointer is automatically set to the next register, so the following read()-command reads from register 3.

Monday, 17 February 2014

A free tool to draw circuits

(Deutsche Version) Today, I want to refer to a useful program:
TinyCAD
With this program, you can draw circuits on a very easy level and export them as an image. You do not need to create an account or anything like that. The program is very simple and does exactly what you want so you do not need to take 100 tutorials to being able to draw a simple circuit.

Control LED strip with ATmega / Arduino

(Deutsche Version) LED strips have been gaining more and more popularity within the last few years. There are even LED strips for less than 20 euro from china. Usually, an LED strip consists of a control unit, a power supply and a remote. For most people, this is sufficient. Anyone who wants to have more (e.g. run through sequences) has to put in a little effort.

Let's primarily face the structure of the strip. There are four connections, one of them is for the + 12 V power supply, the other three connections are for the individual colours. The strip is working like this: If you apply + 12 V and then connect ground to one of the colour connections, this colour is shining. The brightness is now controlled vie PWM. To fully control the LED strip you need:
  • A microcontroller (ATmega / Arduino)
  • 3 bipolar transistors (NPN) which can handle enough current!
The microcontroller sends 3 PWM signals to the base of the transistors (apply pre-resistors, I use 470 Ohms). The emitter of the transistors is connected to ground (a common ground of the 12 V power supply and the microcontroller), the collector is connected to the corresponding colour channel.

To control a single color, you have to execute:


analogWrite(8, 127);

This command will set the transistor which is connected to pin 8 to 50% (255 is the maximum), so the connected colour has 50% brightness. You can only use PWM channels for this (in the data sheet usually referred as OCR).


To achieve any color, you have to mix the colors. therefore, you have to choose a colour in the RGB space, e.g. orange: this colour is R: 255, G:255, B:0. Now you have to program this into the microcontroller:


analogWrite(RED_PIN, 255);
analogWrite(GREEN_PIN, 255);
analogWrite(BLUE_PIN, 0);  

It could occur that different colours are seen at different brightness, you should test this and then set the maximum limit e.g. for green to 127 (green appears very bright for the human eye, therefore the green colour could be too bright.

Sunday, 16 February 2014

"Home Automation" with Arduino and 433 MHz - The complete remote control

(Deutsche Version) I finally merged the control for my shutters and the power outlet into one program and built a small remote control.


Hardware

There is not much to say about the hardware:

It's an Attiny44A, a pullup resistor for the RESET-pin, three buttons (up, down, stopp), a button cell holder and the 433Mhz transmitter. Each button is connected toan input of the Attiny. If the button is triggered, the input is pulled to ground.

Software

The functions for controlling the shutters and the power outlet have been explained in my other posts (unfortunately at the moment these posts are only available in german), so I'm not going to explain the following source code in detail.

#include <RCSwitch.h>
#include <avr/sleep.h>
#include <avr/wdt.h>

RCSwitch mySwitch = RCSwitch();
unsigned char buttonDown = 10;
unsigned char buttonStopp = 9;
unsigned char buttonUp = 7;
unsigned char buttonPressed = 0;

char stopRequest = 0;

void setup() {
  //disable interrupts
  cli();

  //initialize pins
  pinMode(buttonDown, INPUT_PULLUP);
  pinMode(buttonStopp, INPUT_PULLUP);
  pinMode(buttonUp, INPUT_PULLUP);

  //initialize transmitter and switch off power outlet
  mySwitch.enableTransmit(0);  //transmitter is connected to pin 0
  mySwitch.setProtocol(1);
  mySwitch.switchOff("11011", "10000");

  //save energy!
  ADCSRA &= ~(1<<ADEN); //disable ADC
  ACSR = (1<<ACD); //disable Analog Comparator

  //initialize Pin-Change-Interrupt
  PCMSK1 |= (1<<PCINT8); //Pin-Change-Interrupt at pin 2 (Arduino Pin  10)
  PCMSK1 |= (1<<PCINT9); //Pin-Change-Interrupt at pin 3 (Arduino Pin 9)
  PCMSK0 |= (1<<PCINT7); //Pin-Change-Interrupt at pin 6 (Arduino Pin 7)
 
  //prepare Power-Down-Mode
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);

  //enable interrupts
  sei();
}
void loop() {
  sendCommand();
  powerDown();
} 

void sendCommand() {
  if (buttonPressed == buttonDown) {
    mySwitch.switchOn("11011", "10000");
    delay(750);
    sendCommandDown();
    powerDown(25);
    mySwitch.switchOff("11011", "10000");
    buttonPressed = 0;
  }

  else if (buttonPressed == buttonUp) {
    mySwitch.switchOn("11011", "10000");
    delay(750);
    sendCommandUp();
    powerDown(25);
    mySwitch.switchOff("11011", "10000");
    buttonPressed = 0;
  }
}

//command shutters up
void sendCommandUp() {
  mySwitch.setProtocol(4);
  mySwitch.sendQuadState("0F0F0100QQ0F100F0F0F");
  mySwitch.sendQuadState("0F0F0100QQ0F100F0F1Q");
  mySwitch.setProtocol(1);
}

//command shutters stopp
void sendCommandStopp() {
  mySwitch.setProtocol(4);
  mySwitch.sendQuadState("0F0F0100QQ0F100FFFFF");
  mySwitch.setProtocol(1);
}

//command shutters down
void sendCommandDown() {
  mySwitch.setProtocol(4);
  mySwitch.sendQuadState("0F0F0100QQ0F100F0101");
  mySwitch.sendQuadState("0F0F0100QQ0F100F0110");
  mySwitch.setProtocol(1);
}

//let Attiny wait for time secinds
void powerDown(char time) {
  GIMSK |= (1<<PCIE1); //enable Pin-Change-Interrupt
  GIMSK |= (1<<PCIE0);
  
  stopRequest = 0;

  for (char i = 1; i<= time*1000; i++) {
    if (stopRequest == 0) {
      delay(1);
    }
  }

  GIMSK &= ~(1<<PCIE1); // disable Pin-Change-Interrupt
  GIMSK &= ~(1<<PCIE0);
}

//set Attiny to Powerdown-Mode until Pin-Change-Interrupt
void powerDown() {
  GIMSK |= (1<<PCIE1); //enable Pin-Change-Interrupt
  GIMSK |= (1<<PCIE0);
  sleep_mode();  //go to sleep
  //software will continue here after leaving sleep mode
  GIMSK &= ~(1<<PCIE1); //disable Pin-Change-Interrupt
  GIMSK &= ~(1<<PCIE0);
}

void checkButton() {
  if (digitalRead(buttonDown) == LOW) {
    buttonPressed = buttonDown;
  }

  else if (digitalRead(buttonStopp) == LOW) {
    stopRequest = 1;
    mySwitch.switchOff("11011", "10000");
  }

  else if (digitalRead(buttonUp) == LOW) {
    buttonPressed = buttonUp;
  }
}

//ISR for PCINT1 and PCINT0 (Pin-Change-Interrupts)
ISR(PCINT1_vect)
{
  checkButton();
} 
ISR(PCINT0_vect)
{
  checkButton();
} 


The remote control is powered by a CR2032 button cell, so the amount of available energy is limited. Since the control isn't doing anything 99.999% of the time, the Attiny enters sleep mode using the powerdown() function. It sleeps until a button is triggered (Pin-Change-Interrupt). To save as much energy as possible, ADC and AC are switched off.

Sunday, 27 October 2013

Arduino-Code on the ATmega 1284P

(Deutsche Version) Running Arduino code on an ATmega 1284P is pretty simple. You only have to execute some small steps. At first, you have to download the following archive:
https://github.com/maniacbug/mighty-1284p/zipball/master
The content has to be extracted to C:\Program Files (x86)\Arduino\hardware\mighty-1284p (respectively the location where you installed the development environment). Afterwards, the development environment has to be restarted. Now, the 1284 can already be selected. I use the Original Mighty 1284p 16MHz. The pin mapping is different, see below (The number in the Parentheses  is relevant for the Arduino):

                      +---\/---+
           (D 0) PB0 1|        |40 PA0 (AI 0 / D24)
           (D 1) PB1 2|        |39 PA1 (AI 1 / D25)
      INT2 (D 2) PB2 3|        |38 PA2 (AI 2 / D26)
       PWM (D 3) PB3 4|        |37 PA3 (AI 3 / D27)
    PWM/SS (D 4) PB4 5|        |36 PA4 (AI 4 / D28)
      MOSI (D 5) PB5 6|        |35 PA5 (AI 5 / D29)
  PWM/MISO (D 6) PB6 7|        |34 PA6 (AI 6 / D30)
   PWM/SCK (D 7) PB7 8|        |33 PA7 (AI 7 / D31)
                 RST 9|        |32 AREF
                VCC 10|        |31 GND
                GND 11|        |30 AVCC
              XTAL2 12|        |29 PC7 (D 23)
              XTAL1 13|        |28 PC6 (D 22)
      RX0 (D 8) PD0 14|        |27 PC5 (D 21) TDI
      TX0 (D 9) PD1 15|        |26 PC4 (D 20) TDO
RX1/INT0 (D 10) PD2 16|        |25 PC3 (D 19) TMS
TX1/INT1 (D 11) PD3 17|        |24 PC2 (D 18) TCK
     PWM (D 12) PD4 18|        |23 PC1 (D 17) SDA
     PWM (D 13) PD5 19|        |22 PC0 (D 16) SCL
     PWM (D 14) PD6 20|        |21 PD7 (D 15) PWM
                      +--------+

Thursday, 24 October 2013

Control RGB LED with ATmega16A

(Deutsche Version) RGB LEDs are pretty interesting (e.g. if they are in an LED strip). Today, I want to explain how to control an RGB LED with an ATmega16A. At first a short introduction to the RGB LED: I use this LED from Tayda Electronics. This LED consists of three different LEDs which are all in the same body. Therefore, there are 3 pins for the different LEDs and a common cathode (-). If you apply a PWM signal to these 3 pins, you can control the color of the LED. I use the ATmega16A because the ATmega8 only has 2 PWM channels, this is not enough for 3 pins. This should cover the basics for an RGB LED, here is the code which runs through the whole colour space:

#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>

int main(void)
{
  DDRA = 0xFF;//Output
  DDRD = 0xFF;
  ICR1 = 256;
  TCCR2 = (1<<WGM20) | (1<<COM21) | (1<<CS20); // PWM, phase correct, 8 bit.
  TCCR1A = (1<<WGM10) | (1<<COM1A1) | (1<<COM1B1); // PWM, phase correct, 8 bit.
  TCCR1B = (1<<CS10);// | (1<<CS10); // Prescaler 64 = Enable counter, sets the frequency
  double rCounter = 255;
  double rMax=255;
  double bCounter = 255;
  double bMax = 180;
  double gCounter = 0;
  double gMax = 70;
  int stages = 0;
  while(1)
  {
    switch (stages)
 {
   case 0:
     bCounter --;
     if (bCounter <= 0)
  {
    stages = 1;
  }
  break;
      case 1:
     gCounter ++;
     if (gCounter >= 255)
  {
    stages = 2;
  }
  break;
   case 2:
     rCounter --;
     if (rCounter <= 0)
  {
    stages = 3;
  }
  break;
   case 3:
     bCounter ++;
     if (bCounter >= 255)
  {
    stages = 4;
  }
  break;
   case 4:
     gCounter --;
     if (gCounter <= 0)
  {
    stages = 5;
  }
  break;
   case 5:
     rCounter ++;
     if (rCounter >= 255)
  {
    stages = 0;
  }
  break;
 }
 OCR1B = (int)(bCounter*bMax*bCounter/255/255);
 OCR1A = (int)(gCounter*gMax*gCounter/255/255);
 OCR2 = (int)(rCounter*rMax*rCounter/255/255);

 _delay_ms(5);
  }

}