Wellys Dev

  • Home
  • Search/Topics
  • Writings
  • About
  • 2022-09-08
    Developing in C for the ATmega328P: Struct - buttons[]

    Where I describe the struct, buttons[] and how to use it to debounce and check for button presses.

    Introduction

    Describing buttons[] is slightly different than describing a function such as analogWrite() and digitalRead() as buttons[] is a struct. The goal of this entry is to provide sufficient instruction as to how to use buttons[]. If you wish to understand more as to how to debounce a button, please review the links mentioned at the end of this entry.

  • 2022-08-29
    Developing in C for the ATmega328P: Function - digitalRead()

    Where I describe the function - digitalRead().

    Introduction

    The function digitalRead() is analogous to digitalWrite(), in the sense, it takes a pin number as a parameter and as compared to setting its value, it returns the pin’s value. The value will either be a HIGH, indicating the value of the pin was approximately 5V or it will be LOW, indicating the value is approximately 0V or GND.

    Background

    Like digitalWrite(), it also requires using pinMode(), however there are two choices to use, INPUT or INPUT_PULLUP. For the most part, its best to use INPUT_PULLUP as it will have the least chance for error. What this means, is that when a pin is in INPUT_PULLUP, its value will always be HIGH unless it is specifically pulled low. For example, pressing a button connected to ground would pull the pin to GND or LOW. When using INPUT, you will need to ensure the device connected to the pin, will either be at 5V or GND.

  • 2022-08-28
    Developing in C for the ATmega328P: Function - pinMode()

    Where I describe the function - pinMode().

    Introduction

    The function, pinMode() is one of the most important and one of the most forgotten of all of the Arduino framework. It is important, as it sets the function of the pin in question. And when its forgotten, hours of debugging may occur before the programmer realizes “the program works, I forgot to setup the pin”.

    The Arduino Reference: Digital Pins also has a good description as to how pins and pinMode() works.

  • 2022-08-28
    Developing in C for the ATmega328P: Function - digitalWrite()

    Where I describe the function - digitalWrite().

    Introduction

    The digitalWrite() function is a basic building block of using a microcontroller. The function will either raise the value of an output pin to 5V or it will lower it to GND (0V), and in doing so will provide current to drive an LED, a motor or any device which requires current.

    The Arduino Reference: Digital Pins also has a good description as to how pins and pinMode() works.

  • 2022-08-25
    Developing in C on the AVR ATmega328P: Frequently Found Errors (FFE)

    Where I attempt to gather in one place, all of the errors found while building (compile/link/locate) and uploading code to the Uno.

    1. Everytime, I run make, I get an error from Makefile like “Makefile:65: *** missing separator. Stop.” or “Makefile:125….”

    A: Make and tabs

    2. I just downloaded the Windows AVR bundle from Zak and I’m getting this wierd error “error: array subscript 0 is outside array bounds”!

    A: GCC 12 AVR array subscript 0 is outside array bounds

  • 2022-08-24
    Developing in C for the ATmega328P: Functions - Serial input/output

    Where I describe the functions for serial input/out - puts(), getchar() and printf().

    Introduction

    The serial input/output capabilities of C are vastly superior to those in the Arduino framework. In order to communicate via the USB serial cable, you must run a serial monitor program such as the Arduino Serial Monitor, CoolTerm, or moserial. See this page for more details.

    Currently available are:

    • getchar(): reads the next character from the standard input stream and returns it as type int
    • putchar(char): writes a character to the standard output stream
    • puts(string): print a null-terminated string to the standard output stream, ending with a newline character
    • printf(format, variable list): writes to the standard output stream, the list of variables using the formatting string specified. There is considerable detail as to how to use printf below.
    • readLine(*buffer, buffersize) reads a line of text from serial input, either for buffersize number of characters or until CR has been entered. Returns the number of characters read.

    Baud Setting

    While the serial I/O capabilities are polling and not interrupt-based, I’ve found the baud rate can still be set quite high. This is of value when you are attempting to transmit a signficant amount of information such as in examples/pointers. I have successfully set the baud rate to the following speeds in env.make:

  • 2022-08-22
    Developing in C for the ATmega328P: Function - analogWrite()

    Where I describe the function - analogWrite().

    Introduction

    While analogWrite can be an easy function to use, its not easy to initially understand. This stems from a couple of reasons, first, its mis-named in that the function is not doing anything from an analog perspective and second, it uses a concept called pulse-width modulation (PWM), which in itself is difficult to understand.

    Pulse Width Modulation (PWM)

    Lets start with explaining pulse-width modulation. Here is a video where a pulse is being modulated:

  • 2022-08-19
    Developing in C for the ATmega328P: AVR_C Library Functions

    Where I describe how to use the Library functions in AVR_C.

    Introduction

    This page describes where to find specific functions for programming an AVR microcontroller in Standard C using functions similar to the Arduino Language Reference. In this case, the AVR_C framework consists of:

    1. The Standard C Library also known as avr-libc
    2. The AVR_C Library, a GitHub repository developed to mirror the functionality of the Arduino Language

    Standard C Library

    The Standard C Library is provided by AVR Libc. This library is incredibly important as it provides the ability to develop code in Standard C for the AVR family of microcontrollers. It has all of the functionality you would expect if your were using the a Standard C book such as The C Programming Language Kernighan and Ritchie (available in PDF and paper copy). I recommend having a link to the online manual open while developing code.

  • 2022-08-18
    Developing in C on the AVR ATmega328P: Frequently Asked Questions (FAQ)

    Where I attempt to direct all of the questions, pertaining to Developing in C for the Arduino Uno.

    How do I find my Serial port?

    A: There is more than one way!

    CLI methods

    # identify using device
    ls /dev/tty* | grep -E 'ACM|USB'
    
    # identify using tio
    tio -l
    
    # if you have multiple devices, using tio will provide more information
    

    GUI methods

    1. Open the Arduino IDE and go to Tools -> Port and write down the port which has (Arduino Uno) after it. If you aren’t using an Uno, it might not identify itself. Which means you might have to guess, or unplug your board and see which one goes away.
    2. Open CoolTerm, it will provide a drop-down of devices which can be connected to by, CoolTerm.

    Why is learning C important for the Arduino Uno?

    A: C is the ideal combination of a common language and a language which is “close” to the hardware. For example, the AVR assembly language allows writing directly to the hardware (the best example of “close”), except it is not commonly known or easily understood. On the other hand, Python is widely known and easily understood, however, due to it’s requirement to be compiled on-board, it is too large to fit into the memory of the ATmega328P (the microcontroller used by the Arduino Uno). The Arduino approach is to use C++, however, C++ is a little more abstract and requires more memory than C. It’s a good compromise for teaching, however, it is not the best language for embedded development.

  • 2022-07-23
    Developing in C for the ATmega328P: Build

    Where I describe the process of developing code for the Arduino Uno(AVR ATmega328P), specifically, the build process.

    Introduction (from Edit)

    The middle three steps compile/link/locate are typically called the build process, which can simplify the five steps to three:

    1. Edit
    2. Build
    3. Upload

    For a detailed and very worthwhile description of the three steps in Build, I highly recommend this page, Compiling, Link and Locating: Barr Group.

    2. Build (compile/link/locate)

    The image is a typical start-up build sequence, I need to cd three times, to get into the specific folder. Once there, I use make clean to ensure I have a clean start (source code and makefile only) then I execute a make flash.

Page 5 of 8
Copyright © 2025 Lief Koepsel
  • Home
  • Search/Topics
  • Writings
  • About