Showing posts with label Basic. Show all posts
Showing posts with label Basic. Show all posts

Monday, July 18, 2016

Dimmer


This example shows how to send data from a personal computer to an Arduino board to control the brightness of an LED. The data is sent in individual bytes, each of which ranges in value from 0 to 255. The sketch reads these bytes and uses them to set the brightness of the LED.

You can send bytes to the board from any software that can access the computer serial port. Examples for Processing and Max/MSP version 5 are shown below.




Step 1: What You Need?

1 x Arduino Board ( Arduino UNO R3 used in this tutorial.)
1 x LED (Red)

1 x 220 Ohm Resistor 
1 x USB Type B cable 
1 x Mini Breadboard 
Male-to-Male Jumper Wires

Software Required

You can buy Arduino Compatible UNO Ultimate Starter Kit / Learning Kit at here.

Don't have components? Don't worry. Just click the component's name. 

Step 2: Build Your Circuit.

Connect the 220 ohm current limiting resistor to digital pin 9, with an LED in series. The long, positive leg (the anode) of the LED should be connected to the output from the resistor, with the shorter, negative leg (the cathode) connected to ground.


Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.

2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.

Select the right port: Tools >> Port >> Select the port number.

3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 04.Communication >> Dimmer


Click press the "upload" button (see the button with right arrow mark).


Processing Code


The Processing sketch in the code sample above will send bytes out the computer serial port to the board to dim the LED.

Max code

The Max/MSP patch in the code sample above looks like the image below. Copy it and paste it into a new patch window.



Download:



Sources:https://www.arduino.cc/en/Tutorial/Dimmer
Arduino Arduino UNO R3 Basic library

Saturday, July 16, 2016

Smoothing

This sketch reads repeatedly from an analog input, calculating a running average and printing it to the computer. This example is useful for smoothing out the values from jumpy or erratic sensors, and also demonstrates the use of arrays to store data.

Step 1: What You Need?

1 x Arduino Board ( Arduino UNO R3 used in this tutorial.)
1 x 10k Ohm Potentiometer

Female-to-Male Jumper Wires



You can buy Arduino Compatible UNO Ultimate Starter Kit / Learning Kit at here.

Don't have components? Don't worry. Just click the component's name. 

Step 2: Build Your Circuit.

Connect one pin of a potentiometer to 5V, the center pin to analog pin 0, and the the last pin to ground.

Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.


2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.

Select the right port: Tools >> Port >> Select the port number.

3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 03.Analog >> Smoothing



Click press the "upload" button (see the button with right arrow mark).

The code below sequentially stores 10 readings from your analog sensor into an arrays, one by one. With each new value, the sum of all the numbers is generated and divided, producing an average value which then be used to smooth outlying data. Because this averaging takes place each time a new value is added to the array (rather then waiting for 10 new values, for instance) there is no lag time in calculating this running average.

Altering the size of the array used, by changing numReadings to a larger value will smooth the data collected even further.

Download: 

Arduino Arduino UNO R3 Basic library

Fading


This example demonstrates the use of analog output (Pulse Width Modulation (PWM)) to fade an LED. PWM is a technique for getting an analog-like behavior from a digital output by switching it off and on very fast and with different ratio between on and off time.


Step 1: What You Need?

1 x Arduino Board (Arduino UNO used in this tutorial)
1 x LED (Red)
1 x 220 Ohm Resistor 
1 x Mini Breadboard
Male-to-Male Jumper Wires 

Don't have components? Don't worry. Just click the component's name. 

Step 2: Build Your Circuit.

An LED connected to digital output pin 9 through a 220 ohm resistor.

Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.

2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.

Select the right port: Tools >> Port >> Select the port number.




3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 03.Analog >> Fading

Click press the "upload" button (see the button with right arrow mark).

Download: 

Arduino Arduino UNO R3 Basic LED library

Calibration

This example demonstrates one technique for calibrating sensor input. The board takes sensor readings for five seconds during the startup, and tracks the highest and lowest values it gets. These sensor readings during the first five seconds of the sketch execution define the minimum and maximum of expected values for the readings taken during the loop.

Step 1: What You Need?

1 x Arduino Board (Arduino UNO used in this tutorial)
1 x LED (Red)
1 x 220 Ohm Resistor 
1 x Analog sensor (a photoresistor will do)
1 x 10k Ohm Resistor 
1 x Mini Breadboard
Male-to-Male Jumper Wires 

Don't have components? Don't worry. Just click the component's name. 

Step 2: Build Your Circuit.

Analog sensor (e.g. potentiometer, light sensor) on Analog input 2. LED on Digital pin 9. Connect an LED to digital pin 9 with a 220 ohm current limiting resistor in series. Connect a photoresistor to 5V and then to analog pin 0 with a 10K ohm resistor to ground.


Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.

2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.

Select the right port: Tools >> Port >> Select the port number.




3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 03.Analog >> Calibration

Click press the "upload" button (see the button with right arrow mark).



Before the setup, you set initial values for the minimum and maximum like so:

int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value

These may seem backwards. Initially, you set the minimum high and read for anything lower than that, saving it as the new minimum. Likewise, you set the maximum low and read for anything higher as the new maximum, like so:

// calibrate during the first five seconds
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);
// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
This way, any further readings you take can be mapped to the range between this minimum and maximum like so:
// apply the calibration to the sensor reading
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);


Download: 

Arduino Arduino UNO R3 Basic LDR LED library

Analog Write with 12 LEDs on an Arduino Mega


This example fades 12 LEDs up and the down, one by one, on an Arduino Mega board, taking advantage of the increased number of PWM enabled digital pins of this board.

Step 1: What You Need?

1 x Arduino Mega board 
12 x LED (Red)
12 x 220 Ohm Resistor 
1 x Breadboard
Male-to-Male Jumper Wires

Don't have components? Don't worry. Just click the component's name. 

Step 2: Build Your Circuit.

Connect the longer, positive legs (anodes) of 12 LEDs to digital pins 2-13 through 220 ohm current limiting resistors. Connect the shorter, negative legs (cathodes) to ground.


Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.
2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.

Select the right port: Tools >> Port >> Select the port number.


3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 03.Analog >> AnalogWriteMega

Click press the "upload" button (see the button with right arrow mark).


In the setup() function of the code below, a for() loop is used to assign digital pins 2-13 of the Mega as outputs.

Next, in the loop() function of the program below, a trio of nested for() loops are used.

The first of these loops,

for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++)

moves through each of the LEDS one by one, from the lowest pin to the highest. Before this loop is allowed to move from one pin to the next, two things must be accomplished. First, you brighten the individual LED through these lines of code:


for (int brightness = 0; brightness < 255; brightness++) {analogWrite(thisPin, brightness);delay(2);}

With each pass through the loop above, the variable brightness increases by one point, and that value is written to the pin currently selected to the main loop. One that pin reaches the maximum PWM value (255), the following loop kicks in:
for (int brightness = 255; brightness >= 0; brightness--) {analogWrite(thisPin, brightness);delay(2);}

This loop subtracts a point from the brightness variable, dimming the LED back down to 0. Once zero is reached, the main for() loop kicks in, and the program moves on to the next LED pin, repeating all the steps mentioned above.





Download: 



Arduino Arduino UNO R3 Basic LED library

Friday, July 15, 2016

Analog Input


In this example, we use a variable resistor (a potentiometer or a photoresistor), we read its value using one analog input of an Arduino and we change the blink rate of the built-in LED accordingly. The resistor's analog value is read as a voltage because this is how the analog inputs work.



Don't have components? Don't worry. Just click the component's name. 


Step 2: Build Your Circuit.

Connect three wires to the Arduino board. The first goes to ground from one of the outer pins of the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer. The third goes from analog input 0 to the middle pin of the potentiometer.

For this example, it is possible to use the board's built in LED attached to pin 13. To use an additional LED, attach its longer leg (the positive leg, or anode), to digital pin 13 in series with the 220 ohm resistor, and it's shorter leg (the negative leg, or cathode) to the ground (GND) pin next to pin 13.

The circuit based on a photoresistor uses a resistor divider to allow the high impedance Analog input to measure the voltage. These inputs do not draw almost any current, therefore by Ohm's law the voltage measured on the other end of a resistor connected to 5V is always 5V, regardless the resistor's value. To get a voltage proportional to the photoresistor value, a resistor divider is necessary. This circuit uses a variable resistor, a fixed resistor and the measurement point is in the middle of the resistors. The voltage measured (Vout) follows this formula:

Vout=Vin*(R2/(R1+R2))

where Vin is 5V, R2 is 10k ohm and R1 is the photoresistor value that ranges from 1M ohm in darkness to 10k ohm in daylight (10 lumen) and less than 1k ohm in bright light or sunlight (>100 lumen).

Circuit:
Potentiometer:


Photoresistor (LDR):

Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.


2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.

Select the right port: Tools >> Port >> Select the port number.




3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 03.Analog >> AnalogInput

Click press the "upload" button (see the button with right arrow mark).

At the beginning of this sketch, the variable sensorPin is set to to analog pin 0, where your potentiometer is attached, and ledPin is set to digital pin 13. You'll also create another variable, sensorValue to store the values read from your sensor.

The analogRead() command converts the input voltage range, 0 to 5 volts, to a digital value between 0 and 1023. This is done by a circuit inside the microcontroller called an analog-to-digital converter or ADC.

By turning the shaft of the potentiometer, you change the amount of resistance on either side of the center pin (or wiper) of the potentiometer. This changes the relative resistances between the center pin and the two outside pins, giving you a different voltage at the analog input. When the shaft is turned all the way in one direction, there is no resistance between the center pin and the pin connected to ground. The voltage at the center pin then is 0 volts, and analogRead() returns 0. When the shaft is turned all the way in the other direction, there is no resistance between the center pin and the pin connected to +5 volts. The voltage at the center pin then is 5 volts, and analogRead() returns 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

That value, stored in sensorValue, is used to set a delay() for your blink cycle. The higher the value, the longer the cycle, the smaller the value, the shorter the cycle. The value is read at the beginning of the cycle, therefore the on/off time is always equal.

Arduino Arduino UNO R3 Basic LDR library

Analog In, Out Serial


This example shows you how to read an analog input pin, map the result to a range from 0 to 255, use that result to set the pulse width modulation (PWM) of an output pin to dim or brighten an LED and print the values on the serial monitor of the Arduino Software (IDE).

Step 1: What You Need?

1 x Arduino Board (Arduino UNO used in this tutorial)
1 x Potentiometer (10k)
1 x LED (Red)
1 x 220 Ohm Resistor 
Female-to-Male Jumper Wires 

Optional 
Male-to-Male Jumper Wires 
1 x Mini Breadboard

Don't have components? Don't worry. Just click the component's name. 

Step 2: Build Your Circuit.

Connect one pin from your potentiometer to 5V, the center pin to analog pin 0 and the remaining pin to ground. Next, connect a 220 ohm current limiting resistor to digital pin 9, with an LED in series. The long, positive leg (the anode) of the LED should be connected to the output from the resistor, with the shorter, negative leg (the cathode) connected to ground.


Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.



2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.

Select the right port: Tools >> Port >> Select the port number.



3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 03.Analog >> AnalogInOutSerial

Click press the "upload" button (see the button with right arrow mark).


In the sketch, after declaring two pin assignments (analog 0 for our potentiometer and digital 9 for your LED) and two variables, sensorValue and outputValue, the only things that you do in the setup() function is to begin serial communication.

Next, in the main loop, sensorValue is assigned to store the raw analog value read from the potentiometer. Arduino has ananalogRead range from 0 to 1023, and an analogWrite range only from 0 to 255, therefore the data from the potentiometer needs to be converted to fit into the smaller range before using it to dim the LED.

In order to convert this value, use a function called map():
outputValue = map(sensorValue, 0, 1023, 0, 255);

outputValue is assigned to equal the scaled value from the potentiometer. map() accepts five arguments: The value to be mapped, the low range and high values of the input data, and the low and high values for that data to be remapped to. In this case, the sensor data is mapped down from its original range of 0 to 1023 to 0 to 255.

The newly mapped sensor data is then output to the analogOutPin dimming or brightening the LED as the potentiometer is turned. Finally, both the raw and scaled sensor values are sent to the Arduino Software (IDE) serial monitor window, in a steady stream of data.



Arduino Arduino UNO R3 Basic library

Pitch follower using the tone() function


This example shows how to use the tone() command to generate a pitch that follows the values of an analog input. Using a photoresistor (LDR) your Arduino board becomes a simplified light theremin.

Step 1: What You Need?

1 x Arduino Board (Arduino UNO used in this tutorial)
1 x 8 Ohm Speaker
1 x Light Dependent Resistor (LDR) / Photoresistor
1 x Mini Breadboard
Male-to-Male Jumper Wires 

Don't have components? Don't worry. Just click the component's name. 


Step 2: Build Your Circuit.

Connect one terminal of your speaker to digital pin 9 through a 100 ohm resistor, and its other terminal to ground. Power your photoresistor with 5V, and connect it to analog 0 with the addition of a 4.7K resistor to ground.




Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.



2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.

Select the right port: Tools >> Port >> Select the port number.





3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 02.Digital >> tonePitchFollower


Click press the "upload" button (see the button with right arrow mark).
The code for this example is very simple. Just take an analog input and map its values to a range of audible pitches. Humans can hear from 20 - 20,000Hz, but 120 - 1,500 usually works pretty well for this sketch.

You'll need to get the actual range of your analog input for the mapping. In the circuit shown, the analog input value ranged from about 400 to about 1,000. Change the values in the map() command to match the range for your sensor.




Download: 


Arduino Arduino UNO R3 Basic LDR library photoresistor speaker

Playing tones on Multiple outputs using the tone() function



This example shows how to use the tone() command to play different notes on multiple outputs.

The tone() command works by taking over one of the Atmega's internal timers, setting it to the frequency you want, and using the timer to pulse an output pin. Since it's only using one timer, you can only play one note at a time. You can, however, play notes on different pins, sequentially. To do this, you need to turn the timer off for one pin before moving on to the next.

Thanks to Greg Borenstein for clarifying this.


Step 1: What You Need?

1 x Arduino Board (Arduino UNO used in this tutorial)
3 x 8 Ohm Speaker
3 x 100 Ohm Resistor 

Don't have components? Don't worry. Just click the component's name. 

Step 2: Build Your Circuit.



Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.


2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.

Select the right port: Tools >> Port >> Select the port number.


3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 02.Digital >> toneMultiple

Click press the "upload" button (see the button with right arrow mark).

The sketch below plays a tone on each of the speakers in sequence, turning off the previous speaker first. Note that the duration of each tone is the same as the delay that follows it.
Arduino Arduino UNO R3 Basic library speaker

 

  • Copyright © Arduino Tutorial ™ is a registered trademark.
    Designed by Templateism. Hosted on Blogger Templates.