Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

ATMEGA328p Charge-Discharge LED/Capacitor

Status
Not open for further replies.

Athosworld

Member
I have an ATMEGA328p Microcontroller that I want to use as a flip-flop circuit that will flash two LEDs with two 50uF capacitor, similar to the 555 timer version. Is there any way to code this in C, without too many external parts?
02B4504E-6B2B-4CEA-A280-45A4D06666F4.jpeg
 
You will not need 50 uF caps, just a Nano or UNO board and free download
of mBlock.

mBlock takes your block config and generates Arduino code from it. Quick and dirty
for something simple like this. mBlock is free.

Here the two L:EDS alternately flash at a 2 Hz rate. You can set any rate you want.
You can trigger it with a button, or V level or pulse width rate or......

1650506533543.png


Nano board -

1650506665748.png


Of use ATTINY85 (program it with a Nano or UNO board )

1650506724291.png


Regards, Dana.
 
It can be done very simply in C, and component wise just two LEDs with a resistor each to limit current.

In the crudest version just a delay loop for half the time, set the outputs led 1 on and led 2 off, then the same delay again, set the outputs the opposite way, and start over.
 
OK, this is a quick example:

C++:
//
// Simple two-LED flasher example
//
// By Robert Jenkins, for
//
// Electro-tech-online user Athosworld
//

//
// Set L1 and L2 to the pin numbers you are using
//
const int L1 = 0;
const int L2 = 1;

void setup() {
  // put your setup code here, to run once:
 
  pinMode(L1, OUTPUT);
  pinMode(L2, OUTPUT);

}

// There are many ways to do this, but this is the simplest
// and easiest to follow - it's not intended to be most
// compact or code efficient.


void loop() {
  // put your main code here, to run repeatedly:

  // Turn one on at the start of the loop
  // (And the other off)
  // LOW is on, as the LED common is to power

  digitalWrite(L1, LOW);
  digitalWrite(L2, HIGH);

  // Wait half the cycle time,
  // eg. 500mS for a one second cycle
  delay(500);

  // Set the opposite leds ON and OFF
  digitalWrite(L1, HIGH);
  digitalWrite(L2, LOW);
 
  // Wait the other half ofthe cycle time,
  delay(500);

  // The sequence will then continue back at the start of the loop.

}

Video of it running - using a different board to yours (Seeed Xiao M0), but as long as you set the LED pins correctly it should work with any Arduino-compatible device.


ps. I don't generally use Arduinos, I prefer PIC MCUs for most projects. That's a good reminder of why - the Arduino IDE compiler produced 34000 byte of code to do something that should take a small fraction of that..
 
Last edited:
ps. I don't generally use Arduinos, I prefer PIC MCUs for most projects. That's a good reminder of why - the Arduino IDE compiler produced 34000 byte of code to do something that should take a small fraction of that..

That's because you're compiling for a Xiao;
If you had compiled for Uno or Nano only 972 bytes .. ..

MM
 
OIC ..

Well, you're the genius, but .. .. .. .

But with a PIC (a 16F684 as something to select & using MPLab & the CCS C compiler) it take just 137 x 14 bit locations :D

.. .. .. .. is also totally irrelevant to the OP's request ?

MM
 
Yes Rob, but I think you're missing the point !

You're the Genius, I'm the Novice .. .. .. .

As a novice, I gave up asking for help in open forum a long time ago as a result of so many journeys up the garden path.

The OP asked for help with an Atmega328P, not an Arduino; that was raised by Dana, post#2.

You then offered some working code, and demonstrated as functional when loaded into a Seeeduino Xiao, which is not Atmel but ARM-Cortex and would not program the Atmega328P without first identifying and loading a bootloader.

Then the Seeeduino Xiao got a poor press review, citing a PIC 16F684 and two bits of additional software as a better option because of memory occupancy, all three options together requiring something of a steep learning curve and a masters degree in patience to use successfully, and now we're even further away from the OP's question.

IMHO .. up the garden path again !

MM
 
As I said, I'm not in to Arduino-type stuff; however, everything I could find relating to the ATMEGA328p implies the commonest way of programming them is via the Arduino IDE - even when using a bare MCU in a project.

Based on that, the the Arduino program I posted seemed appropriate, as it should be compatible as long as the correct target device was configured.

And even if using a different compiler, the basic principle I described in the post before that is clearly demonstrated & anyone familiar with C should not have a problem adapting it to different compilers.
It all seemed appropriate to the OPs original question ??

Search "ATMEGA328p programming" (no mention of arduino) and see what you think, from the results?
 
Yes, that's all quite correct, but no mention of the need for a bootloader to get your code into the chip, and that is another matter entirely.

Anyway, I made my point even if not accepted; I'll go crawl back under the stone from whence I came !

MM
 
Yes, that's all quite correct, but no mention of the need for a bootloader to get your code into the chip, and that is another matter entirely.
Even with a bare 328 chip (no boot-loader) probably the easiest way to get code in to it is to use an Arduino as a programmer via the (normally unused) programming pins on the Arduino board:D

The Arduino IDE even includes suitable programmer software:

Code:
// ArduinoISP
// Copyright (c) 2008-2011 Randall Bohn
// If you require a license, see
// http://www.opensource.org/licenses/bsd-license.php
//
// This sketch turns the Arduino into a AVRISP using the following Arduino pins:
//
// Pin 10 is used to reset the target microcontroller.
//
// By default, the hardware SPI pins MISO, MOSI and SCK are used to communicate
// with the target. On all Arduinos, these pins can be found
// on the ICSP/SPI header:
//
//               MISO °. . 5V (!) Avoid this pin on Due, Zero...
//               SCK   . . MOSI
//                     . . GND
//
// On some Arduinos (Uno,...), pins MOSI, MISO and SCK are the same pins as
// digital pin 11, 12 and 13, respectively. That is why many tutorials instruct
// you to hook up the target to these pins. If you find this wiring more
// practical, have a define USE_OLD_STYLE_WIRING. This will work even when not
// using an Uno. (On an Uno this is not needed).
//
// Alternatively you can use any other digital pin by configuring
// software ('BitBanged') SPI and having appropriate defines for PIN_MOSI,
// PIN_MISO and PIN_SCK.
//
// IMPORTANT: When using an Arduino that is not 5V tolerant (Due, Zero, ...) as
// the programmer, make sure to not expose any of the programmer's pins to 5V.
// A simple way to accomplish this is to power the complete system (programmer
// and target) at 3V3.
//
// Put an LED (with resistor) on the following pins:
// 9: Heartbeat   - shows the programmer is running
// 8: Error       - Lights up if something goes wrong (use red if that makes sense)
// 7: Programming - In communication with the slave
//
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top