An ATmega328 with two Identities

Some would say that an ATmega328 with two identities is not possible. However, it depends on who you ask.¬† ūüôĄ ¬† First though, before we ask, here is some necessary background information.

Microcontrollers from Atmel (AVR) and Microchip (PIC) have a built in ‘signature’ or identity. This signature is a unique code that identifies its model. When you program a chip (for example, upload a sketch in the Arduino IDE),¬† the process checks that the chip selected matches the type it‚Äôs connected to. This protects the microcontroller from user error. Therefore, a chip has only one built in identity, that is, if you ask it. However, sometimes, a program it runs speaks for it and tells a different story. So an ATmega328 with two identities is possible. Let’s find out how.

While making a new batch of my DIY Arduino UNOs I bought some ATmega328 microcontroller chips. An original Arduino UNO has an ATmega328P microcontroller. The P designation means picopower. There are ‘minor’ differences between the ATmega328 and ATmega328P but for my purposes the ATmega328 works essentially the same as the P version. Even though they function the same, they have different signatures. Since the ATmega328 is not used in an Arduino, that chip is not recognized/used by the Arduino IDE unless we make some changes.

Let’s take an example of how the Arduino IDE burns the bootloader for an ATmega328(P).¬† Here is how things are physically connected:

An ATmega328 with two IdentitiesGet the fritzing diagram HERE.

Behind the scenes, the Arduino IDE uses the AVRDUDE software to do the programmimg (uploading the sketch). AVRDUDE ‘talks’ to various hardware (and software) programmers which physically connect it to the microcontroller chip. The Arduino IDE runs AVRDUDE in the background with specific parameters on how it should run.¬† We are specifically interested in three parameters (-C -c -p). I will leave it up to the reader to consult the AVRDUDE documentation if they are interested in the other parameters.

The -C parameter specifies the file with various microcontrollers AVRDUDE can program and their respective identities. (By the way,¬† this configuration file does specify and recognize both the ATmega328 and ATmega328P.) The -p parameter specifies the part (i.e. the chip) being programmed. The -c parameter specifies the ‘hardware’ programmer being used. That ‘hardware’ programmer could also be emulated in software.

The specific parts of the AVRDUDE command which the Arduino IDE creates to burn the bootloader is similar to the following:

Notice that the part is the ATmega328p and the programmer is the stk500v1. It is not in reality an stk500v1 piece of hardware but in our case it is being emulated in the sketch called ArduinoISP. Looking at the above hardware diagram, notice that the device we are using to program the chip is an Arduino UNO to which we must first upload that ArduinoISP sketch for it to act as an stk500v1. Now comes the identity problem. When we try this command when the physical chip is an ATmega328 and the -p is an ATmega328P we will get the following error:

This is because the sketch (emulating the stk500v1) talks directly to the chip and gets its identity (0x1e9514) which does not match the identity (0x1e950f) which AVRDUDE has in its configuration file. There are several ways to get around this problem.

  1. Copy the command generated by the Arduino IDE and manually run it after changing the -p to atmega328 (instead of atmega328p)
  2.  Edit the AVRDUDE configuration file and change the signature of the atmega328p to match that of the atmega328

Personally, I do not like either of these, specifically due to all the manual effort involved. A better solution is to use a sketch called ArduinoISP_Multi (which is an updated version of the ArduinoISP sketch). You can get the Arduino_Multi sketch HERE. The ArduinoISP_Multi sketch will tell AVRDUDE that the chip identity is 0x1E950F for both the ATmega328P and ATmega328.  Remember that the identity matching is there to protect from putting code into the wrong chip. Since the ATmega328 and ATmega328P are essentially the same, the identity can be ignored for our purposes in this case. So now the bootloader can be burned in and all is well.

Wait a minute you say? If the Arduino IDE only uses the part (-p atmega328p) what happens when you upload a sketch to that new ATmega328 that has the new bootloader. Won’t it give the signature error?

Here is where the two identities come into play. Notice the command that the Arduino IDE builds to upload a sketch:

The part (-p) is ATmega328p but the programmer is different. The -c parameter is arduino (not stk500v1). In this case the programmer that AVRDUDE is talking to is the bootloader. The bootloader was designed to return an identity of 0x1E950F (the ATmega328P) which it does even if it is running on an ATmega328.

So, you can have an ATmega328 with two Identities. The real identity of the chip internally and its ‘fake’ identity returned by its bootloader. Once the bootloader is there, when you load sketches via the Arduino IDE, it thinks its talking to a real ATmega328p even though physically it is an ATmega328.

So in the end, the identity of the chip all depends on who you are talking too. ūüôĄ

If you have the time

The arduino does not have an RTC (Real Time Clock). However, it is trivial to add one.  And if you have the time, you can do a number of interesting things with your arduino, limited only by your imagination.

I am using the DS1307 RTC module which looks  like this:

If you have the timeRTC DS1307 Back View

 

 

 

 

 

 

It uses a CR2032 3V Lithium coin battery to keep the date and time when there is no power source. It is connected to an arduino via 5V, GND, SCL and SDA pins.

Here is the Fritzing diagram which shows how to wire it up:

RTC Module Fritzing DiagramNote: This diagram shows the DS1307 module from Sparkfun.com, however the pin wiring is the same (5V, GND, SCL and SDA) for the module I have. You can download the fritzing source HERE.

When this module is first put into use, the date and time needs set. Rather than write your own code to control the DS1307, it is easier to use an existing library. I am using the RTC library from Adafruit. You can download it HERE.

The sample sketches in the RTC libraries usually have a means of initially setting the clock to a specific date and time. And usually it is set to the date and time of the computer which loads its sketches. It is wise to first make sure that computer is synced via NTP to an NTP server.

Also, that time setting code is initially commented out. You need to uncomment (or change) that code to set the specific time desired then upload the sketch.  Once the date and time is set you must remember to uncomment the code and upload the sketch a 2nd time.  Otherwise, if the original sketch is run each time you run your arduino, the time is reset to the time set in the sketch (not the current time).  For example, this line of code:

rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

gets the date and time from the computer the sketch is compiled on which becomes a hard coded value in the sketch. These values (__DATE__ and __TIME__) are inserted at compile time and not when the sketch is running.  Since it takes a few seconds to compile this code, burn it into the arduino and verify it.. the time will already not exactly match the real exact time. Also, since most of these sketches display the time on the serial console, they are prefaced with this line:

while (!Serial); // for Leonardo/Micro/Zero

which means that if you don’t start¬† the serial console right away the time is going to be off further.¬† My solution to these problems is to have a simple sketch that just sets the time and nothing else,¬† like this:

Then load the sketch from your imagination that uses the RTC.

Connecting an Android Phone to an Arduino

Connecting an Android Phone to an Arduino could not be easier. All you need is an OTG cable. I wrote a previous post here about connecting and controlling an Arduino from your Android device. Previously, I had always connected real Arduinos. This time I tried connecting my DIY Arduino.

 

Connecting an Android Phone to an ArduinoConnecting an Android Phone to an Arduino

 

 

 

Pictured above is my Samsung S5 (G900H) connected to my DIY Arduino via an FTDI converter and a white OTG cable. The FTDI converter would normally be plugged into your Host PC USB port. With the OTG cable, the Android Phone functions as the host. From the screen shot of the phone, you can tell that the Arduino is loaded with the Minipirate sketch. The phone is running the USB Terminal app. Note: some Android phones do not support USB host mode. To check if yours does, I recommend this app.

When I first connected my Arduino to the Phone it did not recognize the connection. I found out that my FTDI chip had an incorrect product id. The product id was all zeros and needed to be 6001. There is a solution to fix this called ft232r_prog. This Linux command line program allows you to reconfigure (i.e. flash) the FTDI chip. It is a few years old and does not compile on my version of Fedora. (At the time of this post, I am using Fedora 24.) It does however compile fine on an Ubuntu distro. I used YUMI to boot into a live Ubuntu image to compile and run it. Here are the commands to install the prerequisites, compile and change the product ID on an Ubuntu system:

After changing the product id,  the USB terminal app connected to the FTDI converter fine.

Connecting an Android Phone to an Arduino allows you portability in the field. You will want a means of re-charging or replacing the phone battery as the Arduino connection does drain the battery faster than normal.

Flashrom and an Arduino at 3.3V

Flashrom is an amazing piece of software and a life-saver when you need it! If you’ve tried to update your BIOS before using manufacturer supplied software and something went wrong, then you know how valuable Flashrom can be. I’ve written about Flashrom and the BusPirate in a previous post HERE. However, I just found out how easy it is to use Flashrom and an Arduino at 3.3V to program (read, write and erase) various ‘flash’ chips.¬† All Flashrom needs is a programmer to interface with. The Arduino (or Atmega 328P) can very easily and economically be made into a Flashrom programmer.

Basically a program is loaded into the Arduino to let Flashrom program through it. I am using frser-duino which Flashrom sees as a ‘serprog’ programmer. Serprog uses the FTDI chip, the serial port and SPI interfaces to do its work.¬†More information about Flashrom using the Serprog ‘programmer’ can be found HERE. You can get the source code for frser-duino HERE.

I am using Fedora (24 as of the date of this post). Here are the commands I used to install the prerequisites and the frser-duino code into my Arduino (a DIY UNO).

Notice that the documented commands are not what I used as they are for a ‘real’ Arduino UNO. Since I am using an external FTDI chip, it needs to specify 115200 BAUD to program the Arduino. However, once programmed, the larger BAUD/buffer rate of 2000000 can be used to flash with. Once the code is loaded via avrdude into the Arduino, you are ready to use it with Flashrom.

One of the ‘problems’ in flashing BIOS chips is that they are usually 3.3V whereas the Arduino is usually 5V. You could use level translation circuitry to reduce the voltage but it turns out there is a much simpler way. The DIY Arduino can be run at 3.3V using an FTDI USB to Serial converter. A DIY version of an Arduino UNO can be easily made on a breadboard, PCB or perfboard.

Here is an example of how an 8-pin Winbond BIOS chip is wired to the Arduino (download the Fritzing source HERE):

 

Flashrom and an Arduino at 3.3V

 

DIY ArduinoHere is a picture of my newest DIY Arduino. It is a bit different than other Arduinos I made in the past and wrote about HERE. You can see the 6 pin ICSP header on the side (to attach the FTDI USB to serial convertor). Two 14 pin female headers are soldered to the base for connectivity to any pin. There is a 4 pin header for power (left side) and a four pin header for ground (right side). This is basically a bare minimum circuit to run the ATMega 328P and function as an Arduino UNO. It runs at either 5V or 3.3V depending on the jumper of the FTDI USB to serial convertor.

 

6 pin dual female cableA 6 pin female-female cable is used to connect the FTDI converter to the 6 pin male ICSP header on the Arduino. The six pins are GND,CTS,PWR,TX,RX,DTR.

 

 

 

FTDI - USB Mini ConverterFTDI USB-Serial Convertor

 

 

 

 

 

The FTDI USB to Serial Converter I use has a jumper to switch between 3.3V and 5V. I got mine from HERE.Sparkfun.com also sells one HERE. I have the jumper on 5V when I load the Arduino with the frser-duino code. Then I switch the jumper to 3.3V when I use Flashrom to program chips that need 3.3V.

Here is a picture of everything wired together:

Arduino Flashrom Programmer

Flashrom and an Arduino at 3.3V

If the BIOS chip is not soldered on, it can be extracted and placed on a breadboard to flash. However, if the BIOS chip is not socketed (i.e. it is soldered on), you may need an SOIC Test Clip or IC Test Hooks.

 

SOIC 8-pin Test ClipIC Test Hooks

 

 

 

 

 

 

 

 

 

Sometimes it is hard to use the SOIC test clip due to other circuitry in the way so I prefer the IC Test Hooks. They have a tiny ‘claw’ which can be clamped onto the leg of the IC.
IC Test Hook Claw

 

Here is an example command to run Flashrom and save the contents of a BIOS chip to a file:

Here is an exmaple command to just identify the flash chip:

here are the results:

As a side note, the BIOS file to flash has to be in the proper format (ROM image). ASUS for example, distributes their BIOS files in CAP format.  The ROM image has to be extracted from the CAP file with a utility like UEFITool. Alternatively, and much easier, just strip the header off the CAP file with this Linux shell script:

TV Video Output from the Arduino

I made another circuit to display TV Video output from the Arduino.

It uses the TVOut library (documented at the TVOut Arduino Playground) which can be used to output an NTSC or PAL video signal. The circuit is actually just a means of adding three resistors to an RCA video cable. Instead of cutting a cable and soldering on the resistors, it is much cleaner to use this circuit. The circuit is very simple, consisting of an RCA jack, three resistors and a three pin PCB connector. Normally, only two resistors are used (a 1K ohm resistor to sync and a 470 ohm to video), however, some TV/video monitors need an additional 75 ohm resistor from video to ground. Therefore, I also added that 75 ohm resistor in my circuit. Here is a picture of the circuit and the Fritzing diagram showing how to hook it up to an Arduino Leonardo:

 

TVOut RCA Jack Circuit Top ViewTVOut Fritzing Graphic

 

 

 

 

 

 

 

If you want diagrams’ Fritzing source file, download it from here.

I tried the TV Video output from the Arduino at first with my Arduino of choice, a Leonardo, and found that it did not work when the Leonardo is powered from a USB cable attached to a PC. The screen flickered too much. When the Leonardo was powered from a wall plug or battery, it functioned perfectly. I found that other Arduinos (the Uno or Mega) work fine from a PC USB power connection. Depending on which arduino you use, you will need to adjust where the video and sync pins are connected. There is a handy chart on the TVOut Arduino Playground documenting the pins used for various arduinos.

For small projects and portability, an LCD is great to use to display output from an Arduino. However, if your next project needs to display data from an arduino so more people can see it (perhaps a large screen TV), you might consider using the TVOut library and this simple circuit.

The TVOut library has a sample NTSC demo sketch. Here are two of the captured TV screens it outputs:

 

TV Video Output from the Arduino TV Video Output from the Arduino

LM317 to output 3.3 Volts

I created a simple circuit using the LM317 to output 3.3 volts from a 5V or greater input voltage. Why? Because, a number of modules for the Arduino (or Pinguino) take 3.3 Volts. It’s true, some of the Arduinos do have a 3.3V voltage regulator (and thus a source of 3.3V), however, that might not have sufficient amperage. Also, if¬† you are a fan of the DIY (Do-It-Yourself)¬† Pinguinos (as I am), you will need a 3.3V source to accompany them since they do not have one.

So, it is quite handy to have a 3.3V power source around! Here is what the circuit looks like:

LM317 3.3 V Circuit Top ViewLM317 Circuit Side View

I save the foam pads, that came with PC motherboards I bought, and cut out pieces to place under the circuits I make to protect against short circuits. The pads are held in place with two-sided sticky tape.

Here Is the Fritzing circuit diagram:

 

LM317 3.3V Circuit Diagram

If you’d like the Fritzing source to the above diagram, download it from here.

The LM317 is an adjustable  linear voltage regulator. It can take an input voltage of 3-40 Volts DC and outputs a fixed voltage from 1.2 to 37 Volts DC. The output is controlled by resistors on the adjustment pin. Here is a handy calculator to calculate the resistance needed to obtain the desired output voltage. I use 545 Ohms (470 + 75) to output the desired 3.3V.

Notice that I have a two pin header or a power jack as input source (on the right of the pictures) and the output is a two pin header (on the left in the pictures).

I tested this circuit using the ESP8266 and the NL6621-Y1 modules that I wrote about in earlier articles. These modules require 3.3V and both worked fine.

I thought about integrating this circuit on the DIY Pinguino boards I made and might do that next time I get around to making more of them. However, I think the stand alone circuit is more flexible to use. For example, you can attach a USB to RS-232 cable (like this):

USB to Serial Cabledirectly to the circuit (5V Input and GND) and to the wireless module (Tx and Rcv from the cable, 3.3V and GND from the circuit)  and not even need an Arduino / Pinguino.

Serial Port Communication via Python

There are many Linux (and Windows) programs ‘out there’ that allow you to do serial port communications.¬† However, I needed some things that those programs did not provide (on my Linux system) so I came up with my Serial Port Communication via Python script. If you want something that you can learn from and/or customize further this script is for you (if you are using Linux that is). Here is the script:

                                Serial Port Communication via Python

It takes from zero to two parameters. Given no parameters, it defaults to 115200 BAUD and will search for ‘active’ serial ports with the name /dev/ttyUSB* or /dev/ttyACM* and use the first one it finds. If you want to override or specify either the serial port or BAUD rate just give it either or both parameters. If you give it both parameters, they can be in any order as the script can tell what is a port versus what is the rate. When running, the script will ‘echo’ any characters coming from the serial port to the console and ‘echo’ any characters typed in from the console to the serial port. Use a Ctrl-C to exit the script. Note that the comments tell you how to change certain features of the program.

With this script you have basic serial port communications (quite usable) and it can be easily added to for enhanced functionality.

Again, there are other serial communication programs/scripts (which run under Linux). Perhaps the most common one used is minicom. Another quite useful program is screen.  Although screen is primarily used as a terminal multiplexer (i.e. using virtual terminals in a login session), it can be used as a serial terminal communication program too. For example:

screen /dev/ttyUSB0 115200

Also note that the Arduino IDE has a built in serial monitor (the newer versions have a serial plotter too) so that serial communications with your Arduino is relatively easy during code development. The Pinguino IDE does not have a serial monitor built in so my script can be used for that sometimes needed functionality.

You may notice that this script is an updated version of one I wrote about before (here) which I used to communicate with the Arduino MiniPirate sketch.

If you do put my script to use or enhance it, i’d be interested to see what features you add. Leave a comment!

FS1000A Wireless RF433 Transmit and Receive Module Pair

I like experimenting with wireless devices, as you can tell from the many posts about them here! ūüėé I am trying out an FS1000A Wireless RF433 Transmit and Receive Module pair this time.

They use use the RF 433Mhz  frequency with ASK modulation. (You can also get these with the 315Mz frequency instead.)  I got mine from Elecrow.com.  They look like this:

FS1000A Wireless RF433 Transmit and Receive Module Pair

 

FS1000A RF433 Transmit and Receive Module PairHere is a graphic on how they work together:

FS1000A Wireless RF433 Transmit and Receive Module PairBasically you can use one Arduino to control the Transmitter and one Arduino to control the Receiver. An Arduino RF library comes in handy for that. In the past, you’d use the VirtualWire library for controlling these modules, now however, there is a better library called RadioHead which supersedes it. Download it from HERE. Once you install it from the Arduino IDE (Sketch -> Include Library -> Add .ZIP Library) there will be a number of examples to choose from. You can use the File -> Examples -> RadioHead -> Ask -> ask_receiver and ask_transmitter sketches.

I modified them a bit as shown below.

Here is the sketch for the Arduino Receiver:

Here is the sketch for the Arduino Transmitter:

As you can tell from the comments in the code, they are very easy to hook up. Both use only three pins (VCC, GND and DATA). Here is the Fritzing Diagram showing how they are connected:

FS1000A RF433 Transmit and Receive ModulesYou can download the custom fritzing parts from here. If you want my fritzing diagram, download it from here.

Updated Note (6/21/2018): In the receiver code, the printBuffer function ‘dumps’ the buffer in HEX (probably for better diagnostics). Here is the code for the receiver, if you want a normal print of the received characters:

NL6621-Y1 Wireless Module

There is a new kid on the block, the NL6621-Y1 Wireless Module from Nufront.  The NL6621 WiFi SOC is powered by a 160 MHz ARM Cortex-M3 (NL6621M). Everything is integrated in the NL6621M SOC including 448KB of RAM.

Here is what the NL6621-Y1 wireless module looks like:

 

The NL6621-Y1 wireless module Back Image The NL6621 wireless module Front Image The NL6621 wireless module PCB Layout

 

 

 

 

Supposedly it is much better than the ESP8266 from Expressif. However, the ESP8266 has a lot of code and documentation for it whereas there is very little information or code for the  NL6621-Y1 wireless module (as of this post).

Nufront does have several GitHub repositories for it here:

https://github.com/NufrontIOT

The common module (NL6621-Y1) seems to have the NL6621_SerialNet_SDK installed as that is what services the serial AT command set which allows the module to be configured via a serial interface.

I have the NL6621-Y1 wireless module from Elecrow and I wanted to see if I could get it working (attached to an Arduino Leonardo) similar to how I attached an ESP8266 in an earlier post.

Hooking it up, hardware wise, is a breeze. Attach the NL6621-Y1 3.3V pin to the Arduino 3.3V pin, GND to GND, NL6621-Y1 TXD to Arduino RXD and NL6621-Y1 RXD to Arduino TXD.

Arduino Leonardo connected to an NL6621The NL6621 Wireless ModuleAt first, I used the multiserial mega Arduino script (HERE) to experiment with the AT commands. That script basically takes characters you type in the serial console and sends them to the 2nd serial port and it echos any data coming from the 2nd serial port to the 1st.¬† I set both serial port BAUD rates to 115200. Sending “AT+HELP” will list all the supported AT commands.

Searching GOOGLE I could not find any information on the AT command set so I took a look at the SerialNet SDK source. Here is a chart showing what I gleaned from that code:

NL6621-Y1 Wireless Module AT commands

Example: Enter DataMODE

============================================================

AT+QUIT

(then telnet to the IP address of the NL6621 on port 8101)

+++ (exits back to CMD Mode)

 

Example: Scan for Wireless APs
============================================================

AT+WSCANAP=127

 

Example: Set NL6621-Y1 to be an AP
============================================================

AT+WQSOPT=2,11,3,3,0                     //SOFTAP,11,ccmp,wpa2,disable
AT+WSCAP=nufront,123abcdef,0        //SSID,password,retries
AT+IPCONFIG                                      //Show IP configuration

 

Example: Set NL6621-Y1 to STATION mode and connect to an AP
============================================================

AT+WQSOPT=0,11,3,3,0                                //Station,11,ccmp,wpa2,disable
AT+WSCAP=your_ap_ssid,yourpassword,5   //SSID,Password,retries
AT+IPCONFIG                                                 //Show IP configuration

 

Here is an Arduino sketch which sets the wireless options and connects the NL6621-Y1 wireless module to my TP-Link router. Note: If you use this code make sure to change the SSID and PASS variables to match your router!

The NL6621-Y1 Wireless Module Arduino Sketch

One thing I don’t like about the NL6621-Y1 is that it does not have a unique (i.e. official)¬† MAC address. When you send this:¬† AT+MAC¬† the result is +OK=MAC:00:01:02:03:04:05.

Anyway, it will be interesting to see what the open source community comes up with to do with this new wireless chip.

How to Power an LED (Arduino vs. Pinguino)

How to Power an LED (Arduino vs. Pinguino) might seem silly, however, if you take a look at the following graphic, you will hopefully notice something interesting. The Pinguino sources the power to the ‘user’ LED differently that does an Arduino. The Pinguino wiring is represented by the green LED and the Arduino wiring is represented by the red LED.

 

How to Power an LED (Arduino vs. Pinguino)

How to Power an LED (Arduino vs. Pinguino)

The Arduino powers the LED via a Pin connection versus the Pinguino which sources power to the LED from VCC. So when you write your code to turn on/off the LED notice also the differences. To turn on the Pinguino user LED the Pin is brought LOW via digitalWrite(USERLED,LOW) and turned off via digitalWrite(USERLED,HIGH). Notice this is the opposite to the Arduino code. To turn on the Arduino user LED (on Pin 13 usually) the Pin is brought HIGH via digitalWrite(13,HIGH) and turned off via digitalWrite(13,LOW).

The Pinguino IDE has a ‘built-in’ variable USERLED which references the pin for the user LED on the target Pinguino board. The Pinguino IDE also has a ‘built-in’ toggle() function:

toggle(USERLED);

to do basically the same as:

digitalWrite(USERLED, !digitalRead(USERLED));

If you code and run the standard ‘blink’ sketch or just toggle() you might not notice or care about the difference in wiring, as the LED blinks (regardless of whether it was on or off initially).

Load more