Monday, July 27, 2020

Arduinot Tutorial - Serial Monitor

Arduino Tutorial - Serial Monitor

Introduction

  • For this project i will show you how to use a useful feature of the arduino; the serial monitor.
    • it can be used to display serial data which is read form or sent to a peripherals
    • it can be used to display the value of an analog signal read by the arduino.
    • it can also be used to do simple debugging. The value of variables and print statements can be displayed.
  • we will be using the same circuit as the previous project.
  • for this project we will display the value read from the light sensor in the arduino's serial monitor

Materials

  • Arduino
  • Breadboard
  • wires
  • (one) phototransistor
  • (one) LED
  • (one) 220 ohm resistor
  • (one) 10k ohm resistor

Circuit

Here is the circuit diagram for this project.

Things to note:

  • This is the exact same circuit from the Light Sensor project.

Code

Here is code for this project. The entire code can be found here

things to note:

  • Serial.begin(9600) This function starts serial communication.
    • 9600 is the baud rate.
      • baud rate is the rate of serial data transfer in bits per second.
    • for more information, please see the arduino reference page.
  • Serial.print() this function prints to the serial monitor.
    • This will print any message in between the parenthesis.
    • it does not print a new line at the end.
  • Serial.println() this function prints to the serial monitor.
    • This is similar to the print function, except that it prints a new line at the end.

Accessing The Serial Monitor


How to access the Serial Monitor

  • To access the Serial monitor, first make sure the arduino is plugged into your computer.Next you will need to go to 'Tools' on the top bar of the IDE, then select 'Serial Monitor.'
  • next you will see a window pop up. This is the serial monitor window.

Serial Monitor window

Final Conclusions,Remarks,and Results

  • Once the monitor is open and your circuit is set up, you should see data being printed to the monitor window.
  • try changing the amount of light received by the light sensor and checking the changing output on the monitor window.
  • the serial monitor is a useful feature of the arduino and can come in handy in many situations.

Going further

Things to do to continue this project:

  • try printing out different messages to the serial monitor
  • just play around with it and you'll quickly learn everything you may need to know about using it.

Thank you for reading through my tutorial. Please check back again for the next part of my arduino tutorial.

Arduino Tutorial - Light Sensor

Arduino Tutorial - Light Sensor

Light Sensor

Introduction

  • For this project, we are going to use a light sensor to detect the light levels then turn on an LED when it is dark.
  • For the light sensor, we are going to use what is called a phototransistor.
    • They are a form of bipolar transistor which are sensitive to light sources.
  • For an overview of phototransistors, please see this article

Materials

  • Arduino
  • Breadboard
  • wires
  • (one) phototransistor
  • (one) LED
  • (one) 220 ohm resistor
  • (one) 10k ohm resistor

Circuit

Here is the circuit diagram for this project.

Things to note:

  • make sure the long leg(anode) of the phototransistor is connected to 5v of the arduino
  • the short leg(cathode) of the phototranistor is connected to the 10k ohm resistor and to A1 on the arduino.
  • the phototransistor creates a current proportional to the light level. pin A1 is reading the voltage level at the 10k resistor due to this current.

Here is the circuit i built for this project

Code

Here is code for this project. the entire code can be found here

things to note:

  • the analog in pins(A0 - A5) do not need to be set up with pinMode as an input pins.
  • analogRead() is a function which reads in an analog value.
    • it reads a 10 bit number between 0 and 1023
    • in our case, 0 would be no light and 1023 would be the brightest light which the phototranisotr is capable of resolving
    • for more on analogRead(), please see the article here

Final Conclusions,Remarks,and Results

  • if all went well, the LED should light up when it is dark. you may need to adjust the value in the if statement to get it to work correctly in your case.
  • analogRead() can be used to read in an analog signal, but you have to remember that it will be between 0 and 1023.
    • this means that you may need to adjust for the analog signal you are reading in.you can do this a few ways, one of them is the map() function.
      • I will be covering this function in a later tutorial.

Going further

Things to do to continue this project:

  • try adjusting the value in the if to only turn on with different levels of light.
  • you could also go the other way, have the LED turn off with certain levels of light
  • try to incorporate what we learned about analogWrite in a previous tutorial to control the brightness of the LED
    • try adjusting the brightness of the LED based on the value you get from the light sensor.

Thank you for reading through my tutorial. please check back again for the next part of my arduino tutorial.

Saturday, July 25, 2020

Arduino - Dimmable LED

Arduino Tutorial - Dimmable LED

Dimmable LED

  • this is the fifth project in my arduino tutorial.
  • for this project we will control how bright or dim an LED is by using a PWM signal.

Introduction

  • so far we have only dealt with basic digital signals, that is, the signal can only ever be either HIGH or LOW (1 or 0)
  • to control how birght or dim an LED is, we will require an analog source, or something similar to analog.
    • in our case, we are not going to use analog signals, but something called a PWM signal.
  • Pulse Width Modulation(PWM) is a way to mimic analog signals but using purely digital sources.
    • a PWM is created by outputting a square ware but repeatedly switching the signals from on (HIGH) to off (LOW).
      • the signal is turned on for a short duration, then turned off for a short duration, then turned on for a short duration and so forth.
      • the ratio of how long the signal is left HIGH vs how long it is left LOW is called Duty Cycle
        • duty cycle is a very important concept when it comes to PWM signals.
    • for more information on PWM and Duty Cycle, please see wikipedia here.

Materials

  • materials needed for this projects are:
    • Arduino
    • BreadBoard
    • wires
    • (one) LED
    • (one) 220 ohm resistor

Circuit

the circuit for this project is fairly simple. here is the circuit diagram




Here is the circuit i built for this project


Things to note:

  • we are using pin 6 for our PWM signal.
    • only certain pins on the arduino are set up to output PWM signals.you can check the table here to see a list of avaliable pins on each arduino board.
    • you can also look at the pin numbers on the arduino. pins with PWM capability are ususally marked with a ~

Code

here is a screnshot of the code for this project. as usual, the complete code can be found here.

things to note:

  • interestingly,to output a PWM you do not need to use pinMode to set the pin as an output.
  • analogWrite()
    • this writes a PWM signal to the given pin
    • duty cycle is controlled by giving a number between 0 (0% duty cycle) and 255 (100% duty cycle).
    • please see here For more on analogWrite()

Results, Conclusions,and Final Remarks

  • if all went well, you should have an LED which slowly gets brighter then turns off before slowly getting brighter again.
  • PWM signals can also be used to control the speed of DC motors.

More to do

to continue on with this project, here are some more things to do:

  • try using diffrent values for duty cycle and see the results.
  • try changing the duty cycle and the delay times to get a smoother brightening of the LED
    • hint: it may help to add more calls to analogWrite() and delay()
  • try making it so that instead of switching off after 100% duty cycle, the LED slowly dims back to being off.

Thank you for checking out my arduino tutorial. if you have any question, suggestions, or comments, please leave a comment below. And check back again for the next part of this tutorial.

Friday, July 24, 2020

Arduino - Binary Counter

Arduino Tutorial - Binary Counter
Binary Counter

Introduction

  • For part four of my arduino tutorial we will be building a binary counter.
  • Binary is a way of representing numbers using base powers of 2.
    • each number in binary is represented by a series of 1 and 0.
    • each 1 or 0 represents a power of two.
      • starting at the rightmost digit, this is 2^0. the digits immediatly to the left is 2^1, then the next digit is 2^2, etc.
      • to convert to base 10, simply add up all the powers of 2 for which there is a 1 in the binary number.
    • for example, the binary number 0101 in base 10 is 5.
      • the 1 at the right most digit repesents 2^0
      • the digit to the left is 0, so it is skipped over.
      • the digit to the left of the first 0 is a 1 which represents 2^2
      • finally, the left most digit is a 0, so it is skipped.
      • so the final number is 2^0 + 2^2 which equals 5
  • for more information on binary numbers, please see wikipedia

Parts needed

  • for this project, you will need the following parts:
    • Arduino
    • BreadBoard
    • (four) LED
    • (four) 220 ohm resistors
    • (one) push button
    • (one) 2k ohm resistor
    • wires

Circuit

here is the circuit diagram for the Binary Counter


  • things to note:
    • each LED is connected to its own 220 ohm resistor to ground
    • each LED is connected to its own seperate pin on the arduino
    • the push button has a pull down resistor connected to ground.

here is the circuit i built for this project.

Code

here is the code for this project. the complete code can be found here

  • the new parts of the code for this project are:
    • counter++
      • the ++ on the end of counter is a post fix increment
      • this is simply a short hand way to update the value of counter by 1.
    • if(counter & 0b01)
      • this code is checking if the binary number for counter has a 1 in the 2^0 place. if so, it executes the code in brackets.
      • & is the bitwise AND operator
      • for information on bitwise AND, please check wikipedia
      • 0b01
        • 0b indicates a binary number. everything after the b is intepreted as a binary number
          • in this case, 0b01 is binary 1
    • delay(150)
      • delay isnt new, but debouncing is.
        • debouncing is a way to prevent the arduino from reading multiple button presses which happen in quick succession.
          • it is quite easy to get multiple quick buton presses when you just intended to press it once. this is a characteristic of buttons and relays

Results, Conclusions, and Final Remarks

  • If all went well, your circuit should count up with each button press.you should see the LEDs count up in binary.
    • since we only have four LEDs the highest number we can count to is 15.
    • when count reaches 16 and higher, we have a situation known as overflow.

going further

further things to do with this project.

  • you can add more LEDs to count to an even higher number
  • using what you've learned about if statments, can you make counter reset back to 0 after it reaches 16?
  • try making it count backwards. start at 15 and go down to zero, decreasing counter by 1 with each button press.
    • hint: you might want to replace the postfix increment ++ with the postfix decrement --
  • once you've got it working correctly, try removing the delay(150). you will see first hand how debouncing improves the performance of this circuit.
  • try having it count up/down only using code, try eliminating the button altogether.

thank you for checking out my tutorial. check back soon for the next part of this tutorial.

Tuesday, July 21, 2020

Arduino - Charge Pump

Arduino Tutorial - Charge Pump

Charge Pump


Introduction

  • For this project i decided to build a very simply charge pump using the typically hobyist materials i have on hand(Arduino,breadboard,through hole capacitors,etc.)
  • So what is a charge pump any how?
    • the short answer is that it is a way to raise or lower DC voltage using capacitors.
    • this design also utilizes a PWM signal
      • a Pulse Width Modultaed signal, or PWM, is a way to get analog like signal from digital sources.
      • check out the Arduino article on it here
      • also check out the wikipedia article on it here
    • for a much more in depth answer, i recomend reading the wikipedia article on charge pumps

Design

  • for this design i am mostly following this video by dave at EEVblog. check out the video for more information.

Circuit

here is the schematic of the circuit i am using.

  • fairly simply schematic as you can see.
  • I'm using pin 6 on the arduino for the PWM signal.
    • if you take a look at the Arduino Documentation here you can see for the uno that pin 6 generates a 980 Hz. PWM signal.
  • note that the blue and red lines do not connect to each other.

here is the picture of the circuit i buiilt for this project

Code

here is the code which i used for this project.

  • The only thing needed was to set pin 6 to output a PWM signal.
    • the arduino is nice for this, just a single line of code.on a typical 16 bit microcontroller it typically takes multiple lines of code to set everything up even for a simple PWM signal like this.

Results and Conclusions

  • using a 5 volt input from the arduino i get a 12.8 volt output across the 51k resistor.
    • this represents a 2.56x increase in voltage
  • as you can see, using a very simple circuit one can acheive a substantial increase in voltage.
    • the actual voltage output can be affected by a few factors such as the capacitance,voltage drop of diodes,frequency of PWM signal, etc.
  • overall this is a interesting little circuit design which can have some practical uses.
    • a typical example would be to use a charge pump to drive a LCD from a low voltage microcontroller

Arduino Tutorial - Two Buttons

Arduino Tutorial - On/OFF buttons

Two Buttons


Introduction

  • this is the third of my arduino tutorial. we will be expanding what we learned in part two to include a second push button. and a bit more control logic in the code
  • This project will involve having an LED turn on and stay on when button 1 is pressed and then turn off only when button 2 is pressed.
  • What you will need:
    • arduino
    • breadboard
    • wires
    • push button
    • (2) LED
    • (1) 220 ohm resistor
    • (2) 2K ohm or higher resistor

circuit

here is the circuit diagram for this project

  • it's identical to the circuit in project two except for the addition of a second push button
    • the second button is connected to pin 12 on the arduino.
    • it also includes 2k pull-down resistor.
  • note that the blue and green wires are not connected, they simply cross over each other.

picture of the circuit i built for this project.


code

screenshot of the code for this project. the full code can be found here

  • you should recognize almost all of this from the previous two posts.
  • the new items are:
    • bool reset = false
      • this is boolean data type
        • bools can hold either true or false.
    • &&
      • this is a logical AND operator.
        • it checks whether the statements on the left hand side and right hand side are true.
          • if they are both true, then the condition in parenthesis is considered false.
          • if one or both are false then the condition in parenthesis is considered false.
    • else if
      • this is another if statement.
        • it only evaluates the condition in parenthesis if the if statment above it is false, otherwise it and the code in its brackets is ignored

congragulations,if everything is working properly you should now have a LED which lights up when button 1 is pressed and turns off when button 2 is pressed.


review

  • learned about logical AND operator
  • learned about boolean values
  • learned how else if statement works
  • got a first taste at digital logic

going further

what can you do to keep going with this leasson

  • make the LED light up/turn off only if both buttons are pressed at the same time.
  • make the LED turn on/off only if both buttons were pressed, but not simultanously.(press button 1 then press button 2)
  • using a delay can you make it so that the LED flashes on then off if button 2 is pressed?

thank you for checking out my tutorial. check back soon for the next part of this tutorial.

Monday, July 20, 2020

Arduino Tutorial - push buttons

Arduino Tutorial - Push Buttons

Push Buttons


Introduction

  • this is the second of my arduino tutorial. we will be expanding what we learned in part one and expanding upon it to include push buttons.
  • A push button is a circuit component which acts like a switch. when the button is pressed down,it creates a closed circuit between to points. when it is not pressed, it acts as an open circuit between two points.
  • What you will need:
    • arduino
    • breadboard
    • wires
    • push button
    • LED
    • (1) 220 ohm resistor
    • (1) 2K ohm or higher resistor

circuit

here is the circuit diagram for this project

  • the positive leg of the LED is connected to pin 4 on the arduino.
  • the 220 ohm resistor connects to the negative leg of the LED and GND pin on the arduino
  • one leg of the push button is connected to 5 volt pin of the arduino
  • the other leg of the push button is connected to pin 8 of the arduino
  • the 2K ohm resistor is connected as a pull-down resistor o the push button
    • when using inputs it is a good idea to use pull-down/pull-up resistors
    • a pull-down resistor pulls the pin down to LOW when it is not being driven HIGH

picture of the circuit i built for this project.

code

screenshot of the code for this project. the full code can be found here

  • you should recognize #define,setup,loop,pinMode, and digitalWrite from the last post.
  • the new items are:
    • digitalRead
      • this function reads the current value of the given pin.
        • in this case it is reading the value on BTTNPIN
    • int bttn
      • this is a variable declartaion.
        • a variable is a way to store data in your program so that you can use it later on in your program.
        • int this declares the type of data.
          • in this case it is an integer type
          • an integer stores whole numbers(non floating point) from -32,768 to 32,767.
        • bttn
          • this is the name of the variable.
            • you can use almost any name you want for your variables.It helps if you use descriptive names or names which relate to what they are being used for.
        • = this is how you assign data to a variable
          • in this case we are assigning the data from digitalRead to our variable bttn
        • if
          • this statement checks what is in the parenthesis to see if it is true or not
            • if it evaluates to be true then it executes the code within the brackets { }
              • in this case it sets LEDPIN to be logic HIGH when the statement is true.
            • if it evalutes as false then it skips to the next part
      • bttn == HIGH
        • the == is how you check if two values are equal.
          • this checks if bttn is equal to the value of HIGH.
          • if so, the statement is considered true.
          • otherwise it is considered false.
      • else
        • the code within the brackets { } is executed when the if evalutes as false.
          • in this case, when false, it sets LEDPIN to be logic LOW.

congragulations,if everything is working properly you should now have a LED which lights up as long as you are pressing the button.


review

  • we learned that a push button is used as a switch to toggle on/off when it is pressed.
  • you learned that a variable is used to stored data
  • an int is a type of variable to hold whole numbers
  • digitalRead is a function which reads the value of a digital pin
  • you leanred what an if/else statment is

going further

what can you do to keep going with this leasson

  • can you figure out how to make this program work without using an if/else statment?
  • you can try to make this program work without using a variable.

thank you for checking out my tutorial. check back soon for the next part of this tutorial.

Sunday, July 19, 2020

Arduino Introduction

Arduino Tutorial

Arduino Tutorial

Introduction

This is the first of a planned series on the Arduino. I plan to walk you through all the basics of using an Arduino from a simple bliking LED to more complicated stuff such as reading and writing seriel data using the I2C protocol. This is my first attempt at wriitng a tutorial of this sort so i apologize if it inst the most polished of tutorials.

What is an Arduino? the Arduino is an open source easily programmable micro-controller and electronic board. the Arduino is popular for hobbyist and for prototyping. It is programmed using an easy to use language which is a basic version of C/C++. One does not need to be an expert in either of those languages to use an Arduino. In this Tutorial I'll show the steps needed to create your first working program using an Arduino.This guide assumes very little in the way of programming knowledge or experience

What will you need

Getting Started

The first project we will make is a simple blinking light. The first step we'll do is to go over the code for it. the following image shows the code and a view which should be close to your own view in the editor you are using. I'll go over what each of these lines does. the complete code can be found here

#define LEDPIN 5

  • the #define LEDPIN 5 statement acts as a simple text replacer. #define tells the complier to look everywhere in the code for the word LEDPIN and replace it with the number 5. This is done for the following reasons:
    • Makes Programming More Convenient
      • enables one to use a word, in this case LEDPIN, rather than remembering a specific number.
    • Makes Reading Code easier
      • when reading through the code, it is easier to understand a word like LEDPIN rather than just a number such as 5.
    • Makes Changes And Updates Easier.
      • if you wish to change the code to use a different pin, you simply need to replace the number 5 in the define statement with whatever pin number you are using. no other changes would be needed and now your code works using a different pin.
  • all lines which start with '//' are comments. they are not executed as part of the program, they serve as comments and documentation written by the programmer to give information about that specific part of the program.

void setup()

  • this is the starting point of every Arduino program. When an Arduino program is run, it starts at the setup function and reads each line, top to bottom, from the opening bracket { until it reaches the closing bracket }.
    • void refers to the return type of the function. void simply means that the function does not return data.
  • pinMode(LEDPIN,OUTPUT);
    • this function, called pinMode, takes in a pin and sets the type of pin, either INPUT or OUTPUT. In this case it sets LEDPIN to OUTPUT.
      • INPUT is used when you wish to set a pin on the Arduino in order to receive a signal on that pin.
      • OUTPUT is used when you wish to have the Arduino send out a signal on that pin

void loop()

  • the loop function is where the Arduino program goes next after finishing the setup function. just like with the setup function, it starts at the opening bracket and runs top to bottom until it reaches the closing bracket.
    • The big difference is that when loop reaches the end of the function, it jumps back to the start of the loop function and runs again. The loop function will continue to do this until the Arduino program is stopped.
  • digitalWrite(LEDPIN,HIGH);
    • this function writes a digital signal to a given pin.
      • In this case, it is writting a logic HIGH to pin LEDPIN.
  • delay(1000);
    • delay function causes the Arduino program to pause for a set number of milliseconds.
      • in this case it pauses for 1000 milliseconds.
  • digitalWrite(LEDPIN,LOW);
    • The call to digitalWrite sets LEDPIN to logic LOW.

Building the circuit

here is a diagram of the circuit you should build


  1. connect one end of a wire to pin 5 of the Arduino and the other end of that wire to an empty row on the breadboard.
  2. connect the positive(anode) side of the LED to the row you connected the wire to.
    • the anode pin is typically the longer of the two
  3. connect one end of the resistor to the row with the LED and the other end of the resistor to an empty row on the bread board.
    • never connect an LED without a resistor
  4. connect one end of a wire to the row with the resistor and the other end of that wire to the GND pin of the Arduino
  5. finally, connect the Arduino to your computer using the USB cable.

here is a picture of what the final circuit should look like. once you have checked to makes sure it is connected correctly you can move on to the next section.

final steps.

Now that you have written the code and connected the circuit correctly, the next step is to compile the code and run it.

  • You should first verify the code.
    • if you are using the Arduino IDE then the verify code button is the one with the red arrow in the picture below.
  • After Verifying the code and getting no errors, you next need to upload the code to the Arduino.
    • if you are using the Arduino IDE then the upload code button is the one with the blue arrow in the picture below.
  • Once the code is successfully uploaded to your Arduino it should start running within a few seconds. if it doesn't, try pressing the reset button on the Arduino.

congratulations, you have built and programmed your first Arduino creation.

Review

so far in this tutorial we have learned:

  • how #define can be used to give meaningful names to things and can be used to make programming a bit easier.
  • setup is the function you use to setup and initialize your program and arduino
  • loop is a function which loops until the arduino is stopped.
  • pinMode can be used to set a pin as either INPUT or OUTPUT
  • digitalWrite is a fucntion which allows you to write either logic LOW or logic HIGH to a pin.
  • delay is a function which causes the arduino program to pause for a given number of miliseconds.

thank you for checking out my tutorial. check back soon for the next part of this tutorial.

Arduinot Tutorial - Serial Monitor

Arduino Tutorial - Serial Monitor Introduction ...