Lesson 9 | Output Devices

Assignment for the week:

- develop a program that uses an output device

- connect an output device to one of your Fab Lab made microcontroller board

- upload the program into one of your Fab Lab made microcontroller boards to show the output device working

- create a page to document your progress of Lesson 9, what did you do, in which order, what are your experiences (problems, solutions, etc.), add a video to    show the output device working, display the code into the page and add the download of it

Used Software:

    - Arduino IDE

Downloads:

    Software:
            - Arduino IDE

    Sourcecode:
             Download

             This zip.File include the .ino file with the example sourcecode.

             Download

             This zip.File include the .ino file with the sourcecode for my program.

Idea:

In this lesson I would like to display the data of the sensors on a display. In mind of the final project, I will directly integrate the display, which I will most likely use. It is a 3.2 inch TFT display with built-in SD card reader. But I won't use the card reader.

Problem 1:

The display is only 3.3V tolerant. But the ATMEGA has a logic level of 5V. This means that the ATMEAG outputs a voltage of 5V as HIGH at its I/O pins. This means, if you connect it directly to the power supply and the I/O pins, it will break. But there are several possibilities to connect the display anyway.

Possible solution 1:

The first possibility would be to use a ready-made logic level converter. Something like this. This is switched between board and display and converts the HIGH level from 5V to 3.3V from the board. But power is lost.

Possible solution 2:

The second possibility would be to build such a logic level converter yourself. Therefore you have to switch two resistors per pin in series. The first must have a value of 4.7Kohm and the second resistor must have a value of 10Kohm. Also with this version some voltage will be lost.

Possible solution 3:

The third possibility would be to supply the ATMEAG with 3.3V instead of 5V. This would not require any additional hardware. This is only possible if the voltage converter is plugged in and not soldered and no external quartz is used. Another advantage is that the ATMEGA consumes less power at 3.3V, which would even be an advantage for my battery-operated project. To check if the latest version is suitable for me, I had to look at the data sheets of my sensors. Each of the 4 built-in sensors would also work at 3.3V. The RFID reader, which I want to install later, even needs a maximum of 3.3V as logic level. It is also important that the FTDI cable we use for programming always has 5V as logic level. Either I would have to disconnect all cables of the display and the RFID reader during programming. But this is especially difficult when installed. Alternatively, I can also use an FTDI programmer that has a built-in logic level converter. Since I have such a programmer at home, I will choose this procedure. Only the Led on the PCB would not work anymore or not correctly. Since this requires an input voltage of 3.3V, it would be supplied with under 2V instead of 3.3V by the series resistor. But since I plan to remove the Led from my final version, I don't think that's so bad.

The Design:

Then I thought up a suitable "design" for my values. My idea is to display the name of the plant in the upper part. Possibly also the Latin name. Below that the values should be shown in a kind of table. I imagined a three-line representation. On the left the type of the value or the unit of measurement should stand. In the middle the measured value should be shown and on the right the optimal value should be shown. I would like to write this value for all plants on an RFID chip and then read it out.

Problem 2:

Unfortunately, my board put a line through my calculation. The board has too few pins to control the display. I need for this more digitals output pins.

Solution:

As an alternative I now use a buzzer which plays a nice melody depending on the programming. I use the speaker GC0351NX from SHOGYO for this.

Wiring the speaker to the board:

First I soldered two DuPont cables to the two connection pads.

Speaker Board
Left Solderpad Ground
Right Solderpad Pin A0

The Speaker and the two solderpads


The Schematic for the speaker


The PCB with the speaker and the photocell




The example code:

Then I tried the example of Arduino.cc on this topic. With this I could see if the speaker and my wiring are correct. I connected one of the cables to ground and the other to pin A0. After uploading the sketch to the board you hear a nice melody. I've made a minor change to the code before and though I define the pin A0 up as buzzerPin.
/*
  Melody

  Plays a melody

  circuit:
  - 8 ohm speaker on digital pin 8

  created 21 Jan 2010
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Tone
*/
#include "pitches.h"

#define buzzerPin A0

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {
  pinMode(buzzerPin, OUTPUT);
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(buzzerPin, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(buzzerPin);
  }
}
void loop() {

}
    


My code

I use parts of the code from the previous lesson. The speaker should play a sequence of notes C4 and F4. But this should only happen if the amount of light falls below a certain threshold. In this case it would be 200. The code is very simple. In the setup the two required pins are each set to the necessary state. The pin photocellPin is declared as input pin and the pin buzzerPin is declared as output pin. In the loop the variable "pcValue" is first set to 0. Then the photocell is read out and stored in the variable "pcValue". In the IF condition it is then queried whether the value is below the threshold value or not. If the value is below the threshold, the function "sound()" is called without parameters. The function "sound()" first plays the tone C4 and then the tone.
/*#-#-#-#-#( Import needed libraries )#-#-#-#-#*/
    #include "pitches.h"
/*#-#-#-#-#( Declare Constants and Pin Numbers )#-#-#-#-#*/
    #define photocellPin PC0 // The pin with the intern name PC0/ADC0 get now the name bphotocellPin/ Pinnr 23
    #define buzzerPin A3 // The pin with teh intern name ADC3 get now the name buzzerPin
/*#-#-#-#-#( Declare objects )#-#-#-#-#*/
/*#-#-#-#-#( Declare Variables )#-#-#-#-#*/
    int pcValue =0; //Value for the Photocell

void sound(){
  tone(buzzerPin,NOTE_C4,4);
  delay(250);
  tone(buzzerPin,NOTE_F4,8);
}

void setup() {
  //PinMode
    pinMode(photocellPin, INPUT);
    pinMode(buzzerPin, OUTPUT);
}

void loop() {

  pcValue =0;
  pcValue=analogRead(photocellPin);

  if (pcValue >260){
    sound();
  }

   delay(2000);
}


Uploading the Code to the Board

Since we successfully burned the bootloader onto the board last week, we can transfer the code to the board quickly and easily. We use a USB cable with a built-in FTDI chip. Alternatively you can use a simple FTDI programmer board.
Programmer Board
VCC 5V Pin
Ground / GND Ground / GND
RX TX
TX RX

After we have wired the board, we can transfer the sketch directly from the Arduino IDE. You have only to choose the right board (in this case Atmega 328 on a breadboard) and the right port and then hit the upload button in the toolbar. How this works in detail you can read in lesson 6.

A video with the example code:




A video with my own code:




Issues:

- no problems during this lesson