Tuesday, May 19, 2015

Digital Potentiometer Arduino Shield

                                           This circuit was tested and worked properly!



One step ahead: in this article we will review the project for an Arduino shield compatible with Arduino Duemilanove, Diecimila and Arduino UNO R1 and R2.


This shield is based on AD5206 digital potentiometer. We can find some very basic applications of the component reviewed in many blogs and sites, including the arduino.cc website “Controlling a digital potentiometer using SPI“. Unfortunately these tutorials focus on the way you can control this chip: the SPI (= Serial Peripheral Interface). Also the sample Sketches – limited to very few lines of code – are very simple but also almost useless.
Also the usage suggestions are very poor: you can control digitally the audio levels of some devices. But then? It’s up to you. Nothing else.
You can find the shield only at BalearicDynamics.
Don’t forget to use the discount code ELEKTRO1008 to get 20% off discount, the special price for Electroschematics.com users.
When I created Digital Potentiometer Shield I thought in the possible range of uses aiming to a sort of cheap “digital potentiometer lab” to carry out actual experiments and applications. So I added more components in the hope that this project will really be an inspiring source. Not necessarily for audio levels control only.

Potentiometer Controls: Analog vs Digital

Why we should use – in a real scenario – a digital potentiometer? As a matter of fact, we should use a digital potentiometer in every case we need a variable resistance controlled by another event. For example, one of the most common ways is changing the volume of an amplified sound depending on an external condition, e.g. the audio volume: with a sound sensor in a room we can adjust the music output level in order to compensate the room noise.
Another example may be a sensor following the movement of a subject to increase the sound level of the neartest speakers. By continuing the exploration of the world of audio controls we will discover almost unlimited possibilities.
The above examples are only feasable with one or more digital potentiometers.
DigiPot Shield (1200px) 005
There is also an interesting class of application cases where the analog potentiometer is filtered by a digital potentiometer.
Let’s suppose, for example, we are using the RC control of a car race: there are two jogs, one for the acceleration and one for the direction both moved manually in the x-y axis directions (i.e. a very simple RC radio control). We should consider many parameters while managing the two jogs, such as the limits of the RC car motors, the maximum supported acceleration, the maximum speed depending on the ground characteristics and so on.
The more experienced is the driver, better he will drive the RC car without accidents, and possibly win the race!

Hacking the RC Controller

Connecting the manually-controlled analog potentiometer to the analog input of the Arduino board and replacing the analog signals with digital ones on the RC board, we can dramatically change the play roles; correctly configuring the Arduino Sketch every manual input will be filtered by the microcontroller and the RC car will be definitely driven avoiding many of the the above mentioned risks.
But this is only one of the hundred cases which can benefit by a similar circuit.

The Shield Circuit

The schematics circuit connects the AD5206 Digital Potentiomer device to the SPI signals of the Arduino board with the Digital PIN 10 used as Chip Select.
Digital Potentiometer Shield Schematics
There are 6 trimmers connected to the 6 analog PINs on the Arduino board to be used for testing purposes or to generate an analog predefined value (e.g. as a preset level for the microcontroller Sketch). The triggers can be excluded one by one through the dip switch freeing the corresponding Analog PIN for other usages.
That the SPI Clock Signal is also connected to a yellow LED: when it is blinking the communication between Arduino and the AD5206 device is working and the digital potentiometer is receiving data.
Note that the digital potentiometer is internally latched: this means that when the circuit is powered, the device retains the last settings sent.
As the device can work at a reference VCC between 2,7 and 5,5V the jumper JP3 uses the Arduino power supply. When unplugged, an external reference votage can be used. In this case the external reference voltage should be connected to the pin 2 of the connector JP1.
Warning: when connecting an external circuit, the shield circuit GND on pin 1 of the connector JP1 should be connected to the external circuit GND.
Connected in parallel to each potentiometer output of the SD5206 there is a red LED for testing purposes (LED1 … LED6). When connecting an external device, it is preferable to disable the test LEDs disconnecting the jumper JP2. The 6 digital potentiometers outputs are available on the pins 3-8 of the external connector JP1.
The following picture is the PCB layout of the circuit shield.
pcb Layout

Filtering the Analog Potentiometer

The following video shows a simple Arduino Duemilanove moving a servo with an analog potentiometer. The stability is dramatically improved as we connect the digital potentiometer Shield.
The simple circuit used to control the servo with the Arduino Duemilanove does not need special explanations.
arduino servo
The test sketch can be downloaded here.

The sample sketch

The first sketch is useful to test the features of the Shield. It simply replicates the input trigger values generating a proportional value on the corresponding output.
Note: disabling the trigger analog inputs setting the dip switch to OFF, the analog reads of these PINs are random values and the resulting output is unstable.
Opening the serial monitor with the sketch running you will see a loop of input/output values as shown in this screenshot.
DigiPot Sample
Every analog potentiomer (the triggers) is shown with the corresponding resistance value and analog read values, followed to the converted digital output values.
On top of the sketch there are the definitions of the program constants.
  1. #define MAXPOT 6 // Max number of potentiometers
  2. #define SERIALMON // Undef SERIALMON to avoid serial output. For testing purposes only
  3.  
  4. // Units to show the potentiometer resistance value.
  5. #define KOHM 0 // Set to 0 if the resistance should be in Ohm instead of K
  6. // Units to show the digital potentiometer resisance value.
  7. #define DKOHM 1 // Set to 0 if the resistance should be in Ohm instead of K
  8.  
  9. // Test analog potentiometers values in Ohm
  10. #define ANALOGPOT 1000.00
  11.  
  12. // Digital potentiometer value in Ohm
  13. #define DIGITALPOT 100000.00
  14.  
  15. // Steps divider from analog converter
  16. #define ANALOGDIVIDER 1024.00
  17.  
  18. // Steps divider of the digital potentiometer
  19. #define DIGITALDIVIDER 256
  20.  
  21. // SPI Clock frequency. Higher is the clock frequency, higher is the digital potentiometer
  22. // response. Value is expressed in reading-per-second
  23. // The frequency calculation is an integer rounded.
  24. #define DIGIPOT_FREQ 8
Changing the DIGIPOT_FREQ you can see how the clock LED changes the blinking frequency. You can disable the serial monitor that forces the program running slow by replacing the definition #define SERIALMON with #undef SERIALMON
Every step of the Sketch is documented in the code.

The video sketch

The second sketch is the one you see in action in the above video. In this case a simple linear filter is applied to the acceleration behavior. It is important to note as four triggers of the analog input are used to control the behavior of the digital output potentiometer.
DigiPot Monitor
Running the program with the serial monitor you get the output settings as shown in the screenshot above.
You can find the shield only at BalearicDynamics. Don’t forget to use the discount code ELEKTRO1008 to get 20% off discount,

Should I Upgrade to the latest Arduino IDE

If you have been working with Arduino you may notice a small popup that tells you about a new update for Arduino IDE.
You may wonder if there are problems with getting the latest software because as you know its old systems may not work with the new software.
Recently an update came out for Arduino IDE 1.6.1. This version has quite a bit of changes from the old one and there are a few things you should know to help you get up to date on what has changed in this version. As for compatibility, it really depends what you are doing. Libraries should function the same but projects that go beyond just downloading to an Arduino UNO may have some issues.
For example, if you are programming an Attiny85 you may have to do some work to get it to work properly on this new software. I have confirmed though, that if you are using a teensy or sometimes called a teensyduino you are in luck because there is support for the new Arduino IDE. If you are using anything else not made by Arduino you may just have to do a quick Google search, I am sure there is plenty of help on any issues you may have.
So what lies in this new IDE software? First of all there was a BETA version out for quite a while and had some of the feature I will talk about, if you had version 1.0.6 all this will be new for you. Instead of listing all of the changes in a long list, I picked a couple to discuss because I feel that these are most important.
First upon installing the latest Arduino IDE you will notice that you will have to reinstall or update your drivers for your Arduino UNO. I do not know about other devices but I found it interesting that without the updated drivers my Arduino UNO would not show up in ports under tools. As far as I know, there were no fixes for the drivers other than making them compatible with the new IDE.
Another visible difference in this new IDE is the grouped boards and Port types. This feature is quite nice as it contributes to a cleaner look of the Arduino IDE. It is also easier to select the board you want to program. Though it is nice, I found that the Tools tab takes forever to pop up. This part is a little frustrating because on my computer it takes close to five seconds to open this tab up. When I am programming multiple Arduinos it is very frustrating to have to wait this long. It feels as if I did not click the tab. Sometimes I am fooled into clicking the tab again and it ends up not opening at all. I am assuming that this loading is caused by a scan that is done to find all the Arduinos plugged into the computer.
arduino results window
There are plenty of other small updates and fixes that this new version has like an option for 38400 baud rate, more info on memory usage when sketch is compiled, new libraries, and much more. You can actually read exactly what has been changed on Arduino’s website (http://arduino.cc/en/Main/ReleaseNotes ). The final question is do you need to upgrade to the new Arduino IDE, of course because along with the visual changes there is a lot done on the insides to perfect libraries and fix bugs that will cause trouble in your Arduino experimentation.

Sunday, May 17, 2015

Arduino HC-SR04 (with LCD) distance mesurement

Hello,
In this tutorial I am going to show you how to 1. Connect the HC-SR04 to an arduino and print the distance to the serial monitor, 2. Connect an LCD and print the results there instead. The sensor works by pinging out an ultrasonic sound we cannot hear. It then starts a timer and measures how long it takes to get that ping back. And that's the HC-Sr04 in a nutshell.
Now I guess we will go right ahead and get started. Remember, first, let's go through the parts we will use.
 
Remove these adsRemove these ads by Signing Up

Step 1: Parts

In this project we will use the following parts:

Arduino Mega (10-15$), This cost can of course be displaced over future projects.
An HC-SR04 (1 $), I bought this for one dollar and there are a lot of ones that cost a lot more. I bought mine from aliexpress, please make a comment if you would like the link to it.
Some jumper cables (I bought mine for 2-3$ (60 pcs) )
And of course, if you are going to utilize an LCD:

A LCD screen (1-4$), depending on whether you get a shield or a standalone one. I use a 16 x 2 alphanumeric one.
Now that you know the parts that we are going to use, let's connect it up.

Step 2: Connecting (Only HC-SR04)

The way I am going to do this is first only wire up the HC-SR04. Then in another step I will add to the project with the LCD and add a little to the code.
Connections are:

Vcc -> Arduino 5V
Echo -> Any digital pin, I used 7.
Trig (Trigger) -> Any digital pin, I used 6.
GNC -> GND

Step 3: Code (without LCD)

I used the NewPing library, which makes your life much easier with this sensor. Download the library files here and see documentation here. This library does not only work with the SR04 but also these ones: SRF05, SRF06, DYP-ME007 & Parallax PING)))™.
The code I used:
#include <NewPing.h>
#define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Creating the NewPing Object.
void setup() { Serial.begin(115200); // Begin serial com. at 115200 baud rate. }
void loop() { delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). Serial.print("Ping: "); Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println("cm"); //If you would like ping in inches, remove "US_ROUNDTRIP_CM" and the backslash, don't forget to rename "cm" to "inches" }

Step 4: Connections with LCD

Instead of using a potentiometer to control the contrast, I will be connecting pin 3 of LCD screen to a PWN pin on arduino, in this case pin two of the arduino mega. I am using the arduino mega for all connections.
Connections: (LCD -> ARDUINO)
Pin 1 -> GND
Pin 2 -> VCC
Pin 3 -> Arduino pin 2
Pin 4 -> Arduino pin 33
Pin 5 -> GND
Pin 6 -> Arduino pin 31
Pin 7 - 10 -> NONE
Pin 11 -> Arduino pin 22
Pin 12 -> Arduino pin 24
Pin 13 -> Arduino pin 26
Pin 14 -> Arduino pin 28
Pin 15 -> VCC through 220 OHM resistor.
Pin 16 -> GND

Step 5: LCD code

This is the code for the LCD to work, the values are displayed in both centimeters and inches. Enjoy!
#include <LiquidCrystal.h> //Importing libraries #include <NewPing.h>
LiquidCrystal lcd(33,31,22,24,26,28); //Creating object
#define ECHO_PIN 7 //Defining pins #define TRIG_PIN 6 #define MAX_DIST 300
int contrast = 100; //Defining contrast
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DIST); //Defining newping object (The HC-SR04)
void setup() { //Setup lcd.begin(16,2); //Change this to the size of your LCD, my happens to be 16x2 lcd.clear(); //Clear screen pinMode(2, OUTPUT); //Setting contrast pin 2 to an output analogWrite(2, contrast); //Setting contrast }
void loop() { unsigned int uS1 = sonar.ping_in(); //Ping in inches unsigned int uS2 = sonar.ping_cm(); // Ping in cm lcd.clear(); //Clearing screen lcd.setCursor(2,0); //Setting cursor lcd.print("Ping: "); //Printing text lcd.print(uS2); lcd.print(" cm"); lcd.setCursor(2,1); lcd.print("Ping: "); lcd.print(uS1); lcd.print(" in"); lcd.setCursor(1,0); delay(1000); //Change this to the update time you want, I just used 1000 ms }

Step 6: Done, finished

That's pretty much it. If you liked the tutorial and it helped you, please make it your favorite, it would mean a lot. Thanks for reading and I hope that you found it helping.
Samuel

WiFi Enabled Arduino - Interfacing with web APIs

Are you familiar with Arduino, but are looking for a little more connectivity in your projects? This Instructable goes over a new (and cheap) wireless module that has hit the embedded world hard - the ESP8266. This little module is a perfect way to hook your Arduino project into your WiFi. What you do with it is entirely up to your imagination. This project demonstrates how to tie into the WeatherUnderground API and get weather data for your area (or anywhere in the world you choose).
Things you'll need for this Instructable:
- Arduino or other microcontroller and a working knowledge of it (UNO clone for $12.19USD or MEGA for $22.94USD)
- An ESP8266 module (Here for $2.42USD)
- Breadboard
- Jumper wires
- Home WiFi
- Wunderground API key (available free here for non-commercial use)
Things you SHOULD have:
- An independent 3.3V power supply (for the ESP8266)
- A 5V to 3.3V logic level shifter
 
Remove these adsRemove these ads by Signing Up

Step 1: Step 1: The ESP8266

This tiny module is the ESP8266. It provides a lot more functionality than just WiFi; it is a full SoC capable of executing code. For the purpose of this Instructable we will be using it purely for its WiFi capabilities though.
The ESP8266 uses a 3.3V power supply. Some people have supposedly run it on 5V, but don't risk it. This means that to "properly" use it with an Arduino you'll need to use a logic level shifter. I have been using it without one for the past couple months without any ill effects, but this doesn't mean that the device couldn't potentially suffer damage at some point. Also, active transmission current can be quite high. I suggest powering this off an external power supply rather than USB. Sometimes the current will spike and the device will fail to power completely, data will come across corrupted, or other unexpected side-effects will occur.
Another thing to note with this device is the CH_PD pin. This pin needs to be driven high for the device to power up. I overlooked this for a long time and it caused numerous headaches.
If you aren't too familiar with the embedded world yet, you should read up a bit on AT commands. These are what facilitate our ability to talk to the ESP8266. Once you have your module you should probably test it with a USB to TTL converter to figure out how everything works, or you can just wait and try running code on your Arduino. A full list of the commands for this module are available here. I found this site very helpful in learning about this module.
Once you get more experienced with the use of this module you can actually upload code to it to control the GPIO pins onboard. This is beyond the scope of this project, but it doesn't mean you can't develop something totally cool all on your own with it.
Important:
Not all the ESP8266s are made equal. Make sure you check the baud of yours. Mine was 115200 baud, but other users report different rates.

Step 2: Step 2: The Arduino

This should be a familiar device to most of you. This is the Arduino. It is an excellent tool for introduction to the embedded world. The Uno will probably be the most familiar sight. It is powered by the Atmel made ATMega328 MCU. The one shown above is the Arduino Mega which uses the ATMega2560. I HIGHLY recommend the Mega as it provides some extra serial ports so you can connect to a program like Putty and several other serial devices at the same time. I used the Mega for testing purposes, then ran the final product on an Uno using I2C to connect to an FPGA
If you've never used an Arduino before now, play with it a bit. A working knowledge of the Arduino will help you immensely when adapting this project to your uses.
If you have a different microcontroller that you like more (TI Launchpad or PIC32 for example) the same principles will apply to using them.

Step 3: Step 3: The Wunderground API

This is the real power behind our project. Weather Underground is an awesome site that provides an API to local weather data for projects just like this! Make sure that you don't go over their limit for API calls though, you might get billed. I chose this API for two major reasons:
1. It is super easy to use. In no time at all you'll be grabbing the weather and displaying it in cool LED powered ways.
2. It has an AUTOIP function. The API will automatically get your location based off your public IP, so as long as you aren't routing through a proxy you won't have to change the API calls at all.
This API provides a wealth of information that you can use for your project. It gives you city name, lat/long coordinates, epoch time, time zone, and all the weather information you could ever hope for. My project focuses on the current weather and temperature, but I'm sure you all can come up with creative ways to use all the information.
When you sign up for Wunderground you'll be given a unique key. This is what allows you to access the data that Weather Underground provides. Once you have your key you'll need to know how to see the data. A call to this API is a simple HTTP GET request, so you can just type a URL into your browser and see what you get!
Let's look at the API call.
http://api.wunderground.com/api/ key here>/conditions/q/autoip.json Make sure you replace with your actual key!!!

This is the basic call for data. Put your key into the URL and fire away. The API will return a .json file with information regarding the weather in your area. Mine is the wunderground.txt at the bottom of this step. Look through it real quick. See how much data they provide? Tons. You can customize the string to get different data too. Forecasts, history, change the location. It's all really up to you. This simple call is really all that we need for this project, so let's move on to the Arduino code.
One neat idea to consider - Attach a Bluetooth module to your IoT-duino and write a little Android/IOS app that lets you change the location in the API get string. Now you can see the weather anywhere in the world!

Step 4: Step 4: The Code

So I'm sure at this point most of you are familiar with the Arduino IDE. We will be using that to upload our weather getter code to the Arduino. I've attached a cleaned and documented version of the code to this step, but we'll go over it a bit here (not laboriously, just enough so you understand what's going on).
The first part of the code is our #defines, just to make life a little easier. We also have a few variable at the top that help us parse out the data a little easier.
setup() Function
Here we go, this is how we connect our ESP to our Arduino. I reserved some space at the top for the string response. This is a hold-over from a previous iteration of code that had repeated stack crashes because the .JSON just overwhelmed my little micro. You can probably throw this line of code out, I kept it. After that you see I start up an I2C (TWI, Wire, SMB, whatever...) connection. This is where my data is actually going. Next comes the Serial. This is the connection to the ESP, notice the 115200 baud. That's SUPER important to get right, otherwise your Arduino is speaking English and the ESP is speaking Cantonese and everyone is confused. You'll notice the AT commands are back, I told you they'd be important! First our code checks to make sure the device is ready by performing a software reset and waiting for the ESP to respond. After that we set the ESP to station mode and allow multiple connections. We can connect to multiple APs, but for our use now we only will be using one of the connections. Following that is an attempt to connect to the WiFi (next function we look at). Once we've successfully (hopefully) connected to the WiFi we push on and connect to our host (in this case Wunderground).
connectWiFi() Function
This function is the meat and potatoes of our code. So what we have here is a command (AT+CWJAP) that is telling our ESP to join an access point. It is followed by the AP SSID and password. If the ESP returns "OK" then we've successfully joined the AP. If not, the connection failed. If you're using a MEGA or other multi-serial microcontroller you can set it up to send the PC messages of what's going on. I'm sending my data over I2C to an FPGA, so I just threw away the responses.
loop() Function
This is the last major portion of the code. This area connects to the Weather Underground API and downloads the data. Once the ESP starts to send data back we push it into the response string variable we declared earlier. I captured the data line by line (starting with " and ending with ,) and looked for the data I needed (temp_f and weather). This keeps the amount of data the Arduino is trying to store/parse at one time down to a minimum.
Other Functions
Most of this code is pretty well documented, so you should be able to figure out what is going on in the rest of it. I go through and look for temp_f and weather. I then convert those values into bytes to send to my FPGA (keeping the size minimal). I converted the weather into a 1 byte value. This gives me up to 256 different weather codes. I then used a simple method to convert the temperature into Binary Coded Decimal and send that to the FPGA in 2 1-byte packets (4 digits total on a seven segment display).
Credits
This code came partially from my own work and partially from that of others. I don't remember all the sources that I gleaned ideas and code from, but if any of you happen upon other work that looks like this, let me know. I'd love to give the credit where it is due.

Step 5: Step 5: The Circuit

This part is fairly simple. If you're like me and not super concerned about the logic level shifter, just connect the two together. Make sure you connect the RX of the ESP into the TX of the Arduino and the TX of the ESP into the RX of the Arduino. If you don't the ESP and the Arduino will both be yelling at each other but nobody will be listening. Connect your VCC pin to 3.3V and your GND to, well, ground. The only sneaky pin here is the CH_PD pin. This is the Chip Powerdown pin. When this pin is low it tells the ESP8266 to power itself down. If you want close control of the ESP tie this into one of your GPIO pins on the Arduino, otherwise just connect it to 3.3V so the ESP is always on. For even more control tie the RST pin on the ESP into the Arduino too. This way, if need be, you can perform a full hardware reset on the ESP.
---------Connections---------
--Arduino---|---ESP8266---
TX | RX
RX | TX
3.3V | VCC
3.3V | CH_PD
GND | GND
------Optional----------------GPIO | RST
GPIO | CH_PD

Step 6: Step 6: The End

Now the end is in sight. You've got the code, the circuit, and the key. Run it and see what happens! If it didn't do what you expected the very first time, don't get frustrated. Take a step back and look at it part by part. If you are using my code exactly, you will need another device using I2C to get the data from the Arduino. Another good method of debugging (if you've got a MEGA) is to insert some Serial.print() statements throughout your code to see what is going on. No embedded project works the very first time. I'm in school for this and it still took me 2-ish weeks to get this all set up and running the way I wanted.
Now you have an IoT device, what you do with it is entirely up to you and your imagination. This project would make for a nice little weather station if you attached an LCD screen to the Arduino and wrapped it in an enclosure. With a little modification you can use this to read data from just about anywhere and send it over the internet to wherever you need it (temperature/humidity sensors in greenhouses?). Want to present a webpage? Tweak some more and this will do it. The fun with embedded projects it the sky isn't even the limit, you can take them to space.
I hope this has been a helpful demonstration on the ESP8266 and how to use it. It's a fun little device with a good price that will get you rolling on an IoT project. Go forth and make!

P.S. - I am embedding this project in another one, a hardware driven LED Word Clock (I use an FPGA as the backbone of the device). Once that project is completed I'll be uploading an Instructable for it as well.

Step 7: Other Considerations

Another option to consider is Arduino on Breadboard. The UNO is powered by an ATMega328, which is available on amazon for around $5USD. This not only cuts cost, but also size. This chip will run the Arduino bootloader so you can program it using the Arduino IDE using another UNO as an ISP. Want to go even further? Download Eagle Cadsoft and spin up your own custom board with some surface mount components for even greater embedded power. It's a little scary at first, but with a little practice you too can be a professional circuit board designer. The internet is full of people who are willing to teach you anything you want to learn about electronics. Just keep a Google tab up and your mind open and there's nothing you can't do. Happy building!
Welcome to My Blog

Popular Post

Blogger templates

Powered by Blogger.

Navigation-Menus (Do Not Edit Here!)

Opt In (Do Not Edit Here)

Followers

kc innovation we direct the fucture

Popular Posts

Social Profile Icons (Do Not Edit Here!)

Subscribe Via Email (Do Not Edit Here!)

- Copyright © Artificial Intelligence -Robotic Notes- Powered by Blogger - Designed by Johanes Djogan -