Placeholder Image

Subtitles section Play video

  • Hi, my name is Massimo Banzi

  • and I like to make things.

  • And welcome to another Arduino

  • tutorial video.

  • Today we're going to learn

  • how Arduino can control software

  • running on your computer.

  • We are going to use

  • a very simple circuit

  • as you can see here,

  • where we are going to read

  • the position of a potentiometer

  • and sending that data over

  • the USB connection to your computer,

  • where a small program

  • written in the processing language

  • is going to be used to change

  • the color of the Arduino logo

  • depending on the position

  • of the potentiometer.

  • So, how does this work?

  • Well, if you noticed it,

  • the Arduino has two chips.

  • One main processor here,

  • that is the one that execute

  • the code that you program

  • in the Arduino IDE.

  • And this other smaller square chip

  • that lets the Arduino processor

  • communicate with the USB

  • bus over to the computer.

  • So, we are going to use

  • for this project

  • something that is called,

  • Serial Communication.

  • Because the Arduino

  • will send data

  • over to the computer

  • one bit at the time

  • at a certain speed we will define

  • in software, and then,

  • the square chip that you see here

  • will convert that into

  • USB data

  • that travels over to the computer.

  • We have used the serial communication

  • in the past

  • in order to visualize the data

  • that comes from the

  • Arduino board with the Arduino IDE.

  • There is a button on the Arduino IDE

  • called serial monitor.

  • If you press that,

  • a small window opens up

  • and then you can see

  • all the data that comes

  • from the Arduino board,

  • visualized scrolling down

  • the small window.

  • Then there is a little menu

  • at the bottom

  • that says normally 9600,

  • and that's for example

  • the speed of communication

  • used by the Arduino

  • to send the data

  • so, the Arduino speed of

  • sending and the Arduino IDE speed

  • of receiving have to be the same

  • in other to keep the data readable.

  • In this case

  • we are going to do more

  • with the data that comes

  • to the computer.

  • Instead of just visualizing it

  • on the screen,

  • we are going to use

  • another software

  • to capture the data

  • and use it to do something

  • and control the software

  • from the Arduino.

  • In order to do this,

  • we are going to use the processing language.

  • Processing is a great software

  • that was a major inspiration,

  • and one of the bases

  • for developing Arduino

  • and it is a great way

  • to learn how to program.

  • You can download the processing IDE

  • for free from the

  • prossesing.org website.

  • After you download it,

  • you should install it according

  • to the instructions you find on the web site

  • and you should go through a couple of

  • tutorials like the overview tutorial.

  • After that,

  • you can load the processing code

  • into the processing IDE,

  • and you can load the Arduino code

  • into the Arduino IDE

  • and then transfer

  • the program on the Arduino board

  • and after the code is loaded,

  • you will see that the TX LED

  • will start to blink

  • signaling that the Arduino is sending data.

  • You can open the serial monitor,

  • see the data scrolling on the screen...

  • after you see that,

  • the Arduino part of the work is done.

  • You can close the serial monitor,

  • then you switch over to the processing IDE,

  • you press the button to start

  • the code on the processing IDE.

  • That will read the data

  • coming from the Arduino

  • from the serial port

  • and then it will use the number

  • to change the background color

  • of the Arduino logo.

  • Now, we should go through the code

  • and see in detail

  • how the two different applications work.

  • Let's build the circuit first.

  • Here you can see that we have

  • a potentiometer

  • plugged into the breadboard

  • and we have

  • one wire going from the potentiometer

  • to the analog input zero,

  • in order to read the value

  • coming from the potentiometer.

  • Then we have two wires

  • connected to the potentiometer

  • going to the plus and minus rail

  • on the breadboard

  • and then we have

  • a red and black wire

  • going from those two rails

  • to the 5V

  • and GND signals

  • on the breadboard.

  • When I change

  • the position of the potentiometer

  • corresponding voltage is

  • coming out of this wire

  • and going into the analog input zero

  • and that data

  • gets converted into a number

  • that gets sent down the USB cable

  • over to the computer

  • and then to the processing code.

  • We will start with the Arduino code,

  • so here you will see

  • that the code is very simple.

  • In the setup()function

  • we have a Serial.begin() command

  • with 9600 as a parameter.

  • This opens up

  • the serial communication between

  • the Arduino and the computer

  • at the speed of 9600 bits

  • per second.

  • Then in the main loop

  • we read,

  • from the analog input zero,

  • using analogRead(A0).

  • The value that we read

  • is between 0 and 1023

  • and we divide that by 4.

  • This is because Serial.write(),

  • which is the function

  • we are using to send the data

  • over to the computer

  • only accepts bytes

  • that have a value

  • that goes between 0 and 255

  • as a maximum value.

  • So, by dividing the result of "analog</